var result = [
[0, 0, 0, -2],
[0, 0, -2, 0],
[0, 0, 0, -2],
[0, 0, -2, 0],
[0, -2, 0, 0],
[0, -2, 0, 0],
[0, 0, 0, -2],
[0, 0, -2, 0],
[0, 0, 0, -2],
[0, 0, -2, 0],
[0, -2, 0, 0],
[0, -2, 0, 0],
[0, 0, 0, -2],
[0, 0, -2, 0],
[0, 0, 0, -2],
[0, 0, -2, 0],
[0, -2, 0, 0],
[0, -2, 0, 0],
[-2, 0, 0, 0],
[-2, 0, 0, 0],
[-2, 0, 0, 0],
[-2, 0, 0, 0],
[-2, 0, 0, 0],
[-2, 0, 0, 0]
];
for (var i = 0; i < result.length; i++) { //remove duplicates
var listI = result[i];
loopJ: for (var j = 0; j < result.length; j++) {
var listJ = result[j]; //listJ and listI point at different arrays within the result array
if (listI === listJ) continue; //Ignore itself
for (var k = listJ.length; k >= 0; k--) { //checks whether the values are different, if they are continue with the loop
if (listJ[k] !== listI[k]) continue loopJ;
}
// At this point, their values are equal so we remove from the result array
result.splice(j, 1);
}
}
document.getElementById("result").innerHTML = JSON.stringify(result);
<div id="result">
</div>
I'm trying to remove duplicates from some data my program creates when it permutes a 2d array, but in some cases it fails to remove all duplicates. I'm a bit lost as to why, the code is short and commented, I've hard coded in the input 2d array under the result variable.