0

I am working with an array of mongodb's ObjectID objects. I must check if this array contains duplicates, and if so, remove them.

Here is the removeDuplicate function I have, which simply loops over an array with two indexes. If two objects are identical, then the splice() function will remove one of them.

function removeDuplicates(array) {
    var a = array.concat(); // Copy object
    for(var i=0; i<a.length; ++i) {
      for(var j=i+1; j<a.length; ++j) {
        console.log(a[i] + " vs " + a[j]);
          if(a[i].equals(a[j]))
            console.log("removed : " + a.splice(j--, 1));
      }
    }
    return a;
}

Let's say I have a simple array with two identical ObjectID objects in it. Then I call the removeDuplicates function, passing the array. Finally, I print out the array itself.

var array = [];

array.push(new ObjectID("56fc227026aed8e74a699b20"));
array.push(new ObjectID("56fc227026aed8e74a699b20"));

removeDuplicates(array);

console.log(array);

Here is the output. As you can see, nothing is removed at the end.

56fc227026aed8e74a699b20 vs 56fc227026aed8e74a699b20
removed : 56fc227026aed8e74a699b20
[ 56fc227026aed8e74a699b20, 56fc227026aed8e74a699b20 ]

What am I doing wrong ?

2 Answers 2

3

You're removing it from the copy, not the original. It seems that you're logging the original one, though, which will thereby still contain the duplicates.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh, you are right. I forgot to catch the new object... My bad
1

splicing an array over and over again is no nice approach, cause it has to allocate with each splice new memory, and copy the result over to the new memory. Then it has to GC the old memory

But you can do that in one step;
how about a different approach:

//an utility to be used as a filter:
function removeDuplicates(v, i, arr) {
    while(i--) if( v.equals( arr[i] ) ) return false;
    return true;
}

var filteredArray = yourArray.filter(removeDuplicates);

2 Comments

This is a pretty smart way to achieve what I want to do. As I will have to ckeck for duplicates each time I insert new values to my array, I think the method you are giving me would be a perfect choice.
not exactly, I think this would be cumbersome. For checking wether an Array already contains some obj I would use this: arr.some( obj.equals, obj ) or arr.some( obj.equals.bind(obj) ). Maybe inline, maybe wrapped into some utility-function, maybe you combine it with an arr.push(), function add(arr, obj){ arr.some( obj.equals, obj ) || arr.push(obj); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.