0

I have not found an answer to the simplest case possible. I know there are lots of questions like that but all of them have a higher degree of complexity. In my case, the arrays are very similar in the following respects:

  • the same number of elements
  • all the elements are of the same kind,
  • the elements consist only of integers 0 and 1

The code I have is:

var mearr = [
  [1, 1, 0, 1, 1, 0],
  [1, 1, 1, 0, 1, 0],
  [1, 1, 1, 0, 0, 1],
  [0, 1, 0, 0, 1, 1],
  [1, 1, 1, 0, 1, 0],
  [1, 0, 0, 1, 0, 1]
];

for (i=0;i<mearr.length/2;i++) {
    var l = mearr[i];
    var r = mearr[i+3];
    var same = (l == r) ? "Same" : "different";
    console.log(l, r, same);
}

I'd assume that the second and fifth elements would return "Same", but they do not.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
Wasteland
  • 4,889
  • 14
  • 45
  • 91

1 Answers1

1

For this case, I would suggest you to .join() and compare as strings!

var mearr = [
  [1, 1, 0, 1, 1, 0],
  [1, 1, 1, 0, 1, 0],
  [1, 1, 1, 0, 0, 1],
  [0, 1, 0, 0, 1, 1],
  [1, 1, 1, 0, 1, 0],
  [1, 0, 0, 1, 0, 1]
];

for (i = 0; i < mearr.length / 2; i++) {
  var l = mearr[i].join("");
  var r = mearr[i+(mearr.length/2)].join("");
  var same = (l == r) ? "Same" : "different";
  console.log(l, r, same);
}

And yes, yay! It worked:

110110 010011 different
111010 111010 Same
111001 100101 different

Note: As you said, this works only for an array with binary numbers like this. For this case only!

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252