1

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.

lorond
  • 3,856
  • 2
  • 37
  • 52
Shardj
  • 1,800
  • 2
  • 17
  • 43

3 Answers3

3

https://jsfiddle.net/h26ro89p/2/

You forgot to backtrack one index when performing the splice:

    result.splice(j, 1);
    j--;

To prevent this in the future you could reverse the loop or even use a while loop. With a bit of old heroic google searching you can find a solution here:

Remove items from array with splice in for loop

Nine Magics
  • 637
  • 2
  • 8
  • 17
0

i will add something shorter with lodash

https://jsfiddle.net/h26ro89p/3/

        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]];


document.getElementById("result").innerHTML = JSON.stringify(_.uniqWith(result, _.isEqual));
Panos K
  • 1,061
  • 12
  • 26
0

Check this fiddle https://jsfiddle.net/c2uk0xow/3/, you'll need JQuery to use $.each function.

var arr = [
      [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]
    ];
    var _tmp_arr = [];
    var result = [];
    $.each(arr, function(i,v)
    {
        if(!_tmp_arr.includes(JSON.stringify(v)))
        {
            _tmp_arr.push(JSON.stringify(v));
        }
    });

    $.each(_tmp_arr, function(i,v)
    {
        result.push(JSON.parse(v));
    });

Note: I had to convert to string and then parse again, in order to compare both results. [-2,0,0,0] === [-2,0,0,0] returned false, but after I stringify it it gave true, "[-2,0,0,0]" === "[-2,0,0,0]".

bmvr
  • 817
  • 2
  • 9
  • 25