Removing duplicates from a JavaScript object array
June 9, 2011
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:
Dont like my method to filter duplicates in an array using JavaScript. Anyone have a better approach? http://bit.ly/mt1H7f #javascript #dojo—
(@vikasrao) June 04, 2011
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.
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).