3

How do I check for equality between the elements of two arrays without using external libraries (and preferably using ES5)?

I want to check for equality between them without caring about the order of the elements. So the two arrays [1,2,3] and [2,3,1] are equal in my situation.

I guess I should write a function

function isEqual(arr1, arr2) {
  arr1.forEach(el => {
    if (arr2.indexOf(el) === -1) {
      return false;
    }
  });

  arr2.forEach(el => {
    if (arr1.indexOf(el) === -1) {
      return false;
    }
  });

  return true;
}
2
  • There are tons of questions here such as what if they are in different length and have duplicate items. Commented Sep 15, 2016 at 17:48
  • Is [1,2,3] and [1,3,3,2] also equal for you? Commented Sep 15, 2016 at 17:48

2 Answers 2

3

You can use JSON.stringify and Array#sort methods. Sort both the arrays ang compare them after converting to JSON string.

function isEqual(arr1, arr2) {
  return JSON.stringify(arr1.sort()) === JSON.stringify(arr2.sort());
}

console.log(isEqual([1, 2, 3], [2, 3, 1]));
console.log(isEqual([1, 2, 3], [2, 4, 1]));
console.log(isEqual([1, 2, 3, 3], [2, 3, 1]));
console.log(isEqual([1, 2, 3, 3], [2, 4, 1, 1]));


Or instead of JSON.stringify you can also use Array#join method( Suggested by @JonCarter ) which makes it little more simple.

function isEqual(arr1, arr2) {
  return arr1.sort().join() === arr2.sort().join();
}

console.log(isEqual([1, 2, 3], [2, 3, 1]));
console.log(isEqual([1, 2, 3], [2, 4, 1]));
console.log(isEqual([1, 2, 3, 3], [2, 3, 1]));
console.log(isEqual([1, 2, 3, 3], [2, 4, 1, 1]));

Sign up to request clarification or add additional context in comments.

3 Comments

I like this -- it's short and elegant. Just a suggestion: using Array.join() might be a little better than JSON.stringify. e.g. arr1.sort().join() === arr2.sort().join()
@JonCarter : wow that would be much better ..... thanks and updated the answer with your suggestion :)
Thanks! Great solution
0

You could use Array#every to assert the values are equal. This is very rudimentary though as uses a strict equality check which has its limitations. But this shows the rough framework for a iterating over the array.

NOTE: Since arrays are index based lists it is assumed that for them to be equal they would need to have equal values at the same index.

var arr1 = [1,2,3,4];
var arr2 = [1,2,3,4];
var arr3 = [1,2,4];
var arr4 = [1,2,2,4];

function isArrEql(a1, a2) {
  return a1.length === a2.length && a1.every(function(v, i) {
    return v === a2[i];
  });
}

console.log('arr1, arr2: ', isArrEql(arr1, arr2));
console.log('arr1, arr2: ', isArrEql(arr1, arr3));
console.log('arr1, arr2: ', isArrEql(arr1, arr4));

2 Comments

But is a1.every((v, i) => v === a2[i]) the same as a2.every((v, i) => v === a1[i])? Don't I have to check both ways?
@Jamgreen That is what the length check is for. If both arrays are the same length then iterating over one and matching the corresponding value from the other array would have the same result.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.