最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Find a unique number in an array - Stack Overflow

programmeradmin5浏览0评论

Can someone explain to me what exactly is this line of code doing?

function findUniq(array) {
  return array.find((item) => array.indexOf(item) === array.lastIndexOf(item))    
}

I want to know what this line is exactly doing:

return array.find((item) => array.indexOf(item) === array.lastIndexOf(item))

What I think is happening here is that for every item inside the array it is paring the first index of that item to the last index of item. it returns the items that equal to eachother.

I don't understand how it is returning the unique array.

If I were to write this function it would be like this:

return array.find((item) => array.indexOf(item) != array.lastIndexOf(item))

However, that doesn't work since it is returning the mon number.

thanks

Can someone explain to me what exactly is this line of code doing?

function findUniq(array) {
  return array.find((item) => array.indexOf(item) === array.lastIndexOf(item))    
}

I want to know what this line is exactly doing:

return array.find((item) => array.indexOf(item) === array.lastIndexOf(item))

What I think is happening here is that for every item inside the array it is paring the first index of that item to the last index of item. it returns the items that equal to eachother.

I don't understand how it is returning the unique array.

If I were to write this function it would be like this:

return array.find((item) => array.indexOf(item) != array.lastIndexOf(item))

However, that doesn't work since it is returning the mon number.

thanks

Share Improve this question edited May 20, 2022 at 14:12 Mr. Polywhirl 48.8k12 gold badges93 silver badges144 bronze badges asked May 20, 2022 at 14:09 TimTim 852 silver badges9 bronze badges 2
  • (not a js person )it is just checking number first index and last index, and if they are equal then it is returning those number. since unique no has same index (first and last index so it is returning unique elements. its time plexity is hish O(n*n) which is high – sahasrara62 Commented May 20, 2022 at 14:12
  • Assuming you understand what Array.find(), Array.indexOf(), and Array.lastIndexOf() do, the first one (with ===) returns the first unique element in the array, and the second one (with !=) returns the first non-unique element in the array. – M0nst3R Commented May 20, 2022 at 14:19
Add a ment  | 

4 Answers 4

Reset to default 7

The Array.prototype.find() method takes a predicate function (a function that returns true or false based on input parameters) and then returns the element provided the predicate is true.

In your case, given the scenario:

var array1 = [1,2,1,3,5,2,3];
var array2 = [1,2,1,3,5,5,3];

function findUnique(array){

  return array.find(
    // The code below runs for every element of the array.
    //    - for each element, it takes the element and checks if first position, is the same as last position in the array
    (item) => array.indexOf(item) === array.lastIndexOf(item) //
  );
}

console.log("For array 1, unique item is: ",findUnique(array1));
console.log("For array 2, unique item is: ",findUnique(array2));

Array.prototype.indexOf(yourArrayElement) returns the position of the first occurrence of yourArrayElement

Oh the other hand, Array.prototype.lastIndexOf(yourArrayElement) returns the position of the last occurrence of yourArrayElement

If yourArrayElement only exists once within the array, the first and last position will be the same and Array.prototype.find() will return that element.

indexOf returns the first found position of the given element in the array whereas lastIndexOf returns the latest position of the given element in the array.

If indexOf === lastIndexOf, it means the first found element is the same as the latest one --> the element is unique in the array.

This can be written two ways:

  1. Match the first occurrence index with the final occurrence index.

const data = ['a', 'b', 'c', 'b', 'a'];

const findUniq = arr => 
  arr.find((item) => arr.indexOf(item) === arr.lastIndexOf(item));
  
console.log(findUniq(data)); // 'c'

  1. Check for an index of -1 for the next indexOf check.

const data = ['a', 'b', 'c', 'b', 'a'];

const findUniq = arr => 
  arr.find((item) => arr.indexOf(item, arr.indexOf(item) + 1) === -1);
  
console.log(findUniq(data)); // 'c'

The first one is easier to read and convey, because it it written to pare the first and last index check. The second one is more obscure, but both work.

The reason that it return a unique number, because the find function loop from the start and from the end on each iteration, so if the index of both indexOf that loops from the start and lastIndexOf that loops from the end is equal it's mean that there are no duplicates across the array beside the current item.

发布评论

评论列表(0)

  1. 暂无评论