Reputation: 978
This question comes up a lot, but my specific situation appears not to be covered by any SO answers I've reviewed.
I have a multidimensional array with some duplicate arrays which I'd like to remove.
[
[8, 12],
[8, 17],
[8, 92],
[8, 12]
]
[8, 12]
appears twice. How can I remove this duplicate?
Upvotes: 0
Views: 69
Reputation: 382
What about the following:
var array1 = [[8,12],[8,17],[8,92],[8,12]];
var array2 = new Array();
for (var i=0; i<array1.length; i++) {
var e1 = array1[i];
var exists = false;
for (var j=0; j<array2.length; j++) {
if (array2[j][0] == e1[0] && array2[j][1] == e1[1]) exists = true;
}
if (exists == false) {
array2[array2.length] = e1;
}
}
the array2 is now array1 without duplicates. this should be way too slow for realtime game programming, but it should work. I am sorry, if I coded something wrong, it isn't intentional. I didn't test the code.
Upvotes: 2