Removing duplicates from a JavaScript object array

Removing duplicates from a JavaScript array is an often encountered problem but after searching around, I have found very few good solutions to this. I wrote a small program but I realized that my solution had a O(n2) complexity. So I asked on twitter if someone knew of a better solution:

http://twitter.com/#!/vikasrao/status/77134502132064256

My friend pointed out that I can use a map. Since a JavaScript object is essentially a map. I came up with:

var arr = [
    { name: "a", age: 24},
    { name: "b", age: 25},
    { name: "a", age: 24},
    { name: "b", age: 25}
];

var newarr = [];
var unique = {};

dojo.forEach(arr, function(item) {
    if (!unique[item.age]) {
        newarr.push(item);
        unique[item.age] = item;
    }
});

console.log(dojo.toJson(newarr)); //prints [{"name":"a","age":24},{"name":"b","age":25}]

Here’s a JsFiddle link for the above code: http://jsfiddle.net/k6v3R/1/

I’m sure there are better solutions out there, but the complexity for the above code is O(n) although I’m thinking I don’t need to use both an object and an array to filter unique items.

Advertisements

2 thoughts on “Removing duplicates from a JavaScript object array

  1. This still isn’t a very good solution. You need to concat all the properties in the object in order to get a truly unique value. It should be if(!unique[(item.name + item.age)])…etc. That way if name is unique and age isn’t, you’ll still get a unique value.

    • Joe, the object is not used. I only used the object to check for duplicates, and if it was unique, I added it to a second array(called newarr).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s