Motombo
Motombo

Reputation: 1787

Removing duplicates from array ( understand the code)

I'm looking at an exercise and having trouble understand how the following works ( I'm trying to remove duplicates from an array)

var arr = ['a','b','c','a','b','d','e','f'];
var uniqueArray = arr.filter(function(item,pos){
   return arr.indexOf(item) == pos; 
});

My attempt to understand

Here item takes on all of our values in arr. Lets go through an iteration First item = 'a' and pos = 0. Ok. now we want to only filter on the basis of if the index of 'a'is the same as 0

Here indexOf(a) == 0.

Great! this is true, lets put it in the new array.

Now lets move forward to where we see a again, namely at pos = 3

arr.indexOf(a) == 3

Wait... Doesent this meet our requirement as well? How does this even remove duplicates?

Upvotes: 1

Views: 377

Answers (3)

Mano Subburaj
Mano Subburaj

Reputation: 1

Hashtable is the best way to eliminate the redundant values Here is the code :

char arr[] = ['a','b','c','a','b','d','e','f'];
//it will contains all 26 places as zero (A - Z)
char hashArray[26]={0} 
int i; //for iteration;

for(i=0;arr[i]!='\0';i++)
{
  //it will subtracte the ascii value of the letter 
  //from 'a' so that we have the values from 0 to 26)
  hashArray[arr[i]-'a']=arr[i]; 
}

for(i=0;i<26;i++)
{
   if(hashArray[i]!=0) //to Ensure the positon has the character
   {
      printf("%c",hashArray[i]); 
   } 
}

Upvotes: -1

kag359six
kag359six

Reputation: 2263

nicael is right. indexOf(item) is just a function that goes through the array and looks for the first time item appears in the array, and returns the position in the array. In your example, if there is an a at 0 and a at index 3, then indexOf('a') will return position 0, while the value of pos is 3, so the filter returns false.

FOLLOW UP:

indexOf() has another parameter called the fromIndex, which lets you start the search from a position other than the beginning of the array. In this case, you can specify to skip over the first time 'a' occurs by doing arr.indexOf('a', 1) which starts the search at position 1, not 0. In this case the function would return true since the next 'a' is at position 3.

Can I use filter on an object?

No, because filter is a specific function of an Array object. You can get the keys of the object by doing a filter on Object.keys(myObject) since keys() returns an array.

Using your example:

var keyArray = Object.keys(myObject); //object can't have duplicate keys

keyArray.filter(function(item, index) {

    return keyArray.indexOf(item) == index; //will never be false 

});

Upvotes: 1

nicael
nicael

Reputation: 19014

indexOf returns just one integer value, and it is the index of the first found item. So, when pos is 3 (and the item is a), indexOf will return 0 (because the first index of a is 0), 0==3 is false and the element will be removed.

Then, when the pos is 4 (and item is b), indexOf returns 2, the index of the first found b.


As for the objects, they can't have duplicate keys. Each new key will automatically overwrite the old one, so there won't be any duplicates.

Look:

var obj = {a:1, a:3, b:2,c:5,b:4};
console.log(obj)

Upvotes: 1

Related Questions