Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
144
rated 0 times [  147] [ 3]  / answers: 1 / hits: 44043  / 11 Years ago, mon, october 21, 2013, 12:00:00

I am trying to figure out an efficient way to remove objects that are duplicates from an array and looking for the most efficient answer. I looked around the internet everything seems to be using primitive data... or not scalable for large arrays. This is my current implementation which is can be improved and want to try to avoid labels.



 Test.prototype.unique = function (arr, artist, title, cb) {
console.log(arr.length);
var n, y, x, i, r;
r = [];
o: for (i = 0, n = arr.length; i < n; i++) {

for (x = 0, y = r.length; x < y; x++) {

if (r[x].artist == arr[i].artist && r[x].title == arr[i].title) {
continue o;
}
}
r.push(arr[i]);
}

cb(r);
};


and the array looks something like this:



[{title: sky, artist: jon}, {title: rain, artist: Paul}, ....]


Order does not matter, but if sorting makes it more efficient then I am up for the challenge...



and for people who do not know o is a label and it is just saying jump back to the loop instead of pushing to the new array.



Pure javascript please no libs.



ANSWERS SO FAR:



The Performance Test for the answers below:
http://jsperf.com/remove-duplicates-for-loops


More From » arrays

 Answers
45

I see, the problem there is that the complexity is squared. There is one trick to do it, it's simply by using Associative arrays.



You can get the array, loop over it, and add the value of the array as a key to the associative array. Since it doesn't allow duplicated keys, you will automatically get rid of the duplicates.



Since you are looking for title and artist when comparing, you can actually try to use something like:



var arrResult = {};
for (i = 0, n = arr.length; i < n; i++) {
var item = arr[i];
arrResult[ item.title + - + item.artist ] = item;
}


Then you just loop the arrResult again, and recreate the array.



var i = 0;
var nonDuplicatedArray = [];
for(var item in arrResult) {
nonDuplicatedArray[i++] = arrResult[item];
}


Updated to include Paul's comment. Thanks!


[#74828] Sunday, October 20, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jalyn

Total Points: 173
Total Questions: 96
Total Answers: 90

Location: Somalia
Member since Mon, Feb 27, 2023
1 Year ago
jalyn questions
;