[Solved] Can anyone explain the following JavaScript code for unique value how is it internally compared with each other [closed]


return arr.indexOf(value) === arr.lastIndexOf(value);

You have an array. Every element in the array has an index. .indexOf() will give you that index. So if an element is unique, the first index it appears at is the same as the last index. If the element would not be unique, the lastIndex will be higher than the (first)Index.

})[0] || -1; // how this works======================= B

This will just return the first element of the filtered array. And if the array is empty or if the first element is falsey ( 0, null, undefined, … ), return -1. Its equivalent to if ( array.length && array[ 0 ] ) { return array[ 0 ]; } else return -1.

So the function in it’s entirity will just return you either the first unique element inside the array, or -1 if all elements have at least one duplicate.


OK, Let’s try again:

1) The function wants to return the first unique element that appears in an array. So if we have an array [ 1, 1, 3, 7, 10, 10 ], the result should be 3, since 1 appears two times in the arrray.

2) So the first step is making sure the array only contains unique elements. We do this by filtering out all the non-unique elements in the array.
To do so, we use Array.filter(), the basic array method to filter elements out of an array. This filter() method expects us to provide a function that will return true or false. The method will then loop over the array, applying the function on each element. All the elements the function returns true for, will stay in the results array. All the elements we return false for, will get removed from the results.

For example: [ 1, 1, 3, 7, 10, 10 ].filter(( element ) => element > 5); In this example we return true if an element is bigger than 5, and false if its smaller than 5. So our result will be [ 7, 10, 10 ]

3) The function we want to actually use here, is to return true for elements that are unique and false for elements that aren’t. An unique element appears only once inside an array. So we can leverage array.indexOf() and array.lastIndexOf(). The former will return the first index an element appears at. The second the last index.

Example: let’s assume our array is [ 1, 1, 1, 9, 7, 10, 7, 21 ] and that the element we’re checking is the value 1.
The .indexOf( 1 ) is 0, since the first appearance of the value 1 inside the array, is the first index, since our array starts with the value 1.
The .lastIndexOf( 1 ) is 2, since the 3rd element of the array is also 1.

Compare this to the value 9.
The .indexOf( 9 ) is 3, since it’s the 4th element of the array.
The .lastIndexOf( 9 ) is also 3, since there’s only one 9 inside the array. So both indexOf and lastIndexOf find that same index.

4) Once we have the index and lastIndex of an element , we then compare them:
arr.indexOf(value) === arr.lastIndexOf(value).
So let’s continue with the previous example: [ 1, 1, 1, 9, 7, 10, 7, 21 ].

For the value 1, the index was 0 and the lastIndex was 2. Compared to eachother, this is false, since ( 1 === 2 ) = false;. So we return false for all the values 1 inside the array and hence, they get filtered away in the result.

For the value 9, the index was 3 and the lastIndex as well. So our comparison becomes ( 3 === 3 ) = true;. So we return true from inside the filter function and hence, 9 gets to stay inside the results.

5) This logic gets used on all the elements in the source array. All the elements where the index isn’t the same as their lastIndex, get filtered away. All the elements where both indices are the same, are unique since they only appear once, and so they can stay.

6

solved Can anyone explain the following JavaScript code for unique value how is it internally compared with each other [closed]