0

Say I have two arrays

["a", "b", "c"]

["c", "a", "b"]

What is the best way to compare these two arrays and see if they are equal (they should come as equal for the above scenario)

Sirko
  • 72,589
  • 19
  • 149
  • 183
bluesman
  • 2,242
  • 2
  • 25
  • 35
  • possible duplicat of http://stackoverflow.com/questions/13523611/how-to-compare-two-arrays-in-node-js – user2009750 Apr 02 '14 at 13:17
  • Clearly a duplicate; the only missing link is pre-sorting both compared arrays first. – raina77ow Apr 02 '14 at 13:19
  • @pronox That solution doesn't work > var arr1 = ["a","b","c"]; undefined > var arr2 = ["c","a","b"]; undefined > if (arr1.length == arr2.length ... && arr1.every(function(u, i) { ..... return u === arr2[i]; ..... }) ... ) { ... console.log(true); ... } else { ... console.log(false); ... } false – bluesman Apr 02 '14 at 13:22
  • then how is it marked accepted and up voted few times, let me check i – user2009750 Apr 02 '14 at 15:21
  • @bluesman its working – user2009750 Apr 02 '14 at 15:29
  • @pronox There was a missing step to sort the array as thefourtheye showed in his solution – bluesman Apr 03 '14 at 08:10

2 Answers2

4
function compareArrays(array1, array2) {
    array1 = array1.slice();
    array2 = array2.slice();
    if (array1.length === array2.length) {       // Check if the lengths are same
        array1.sort();
        array2.sort();                           // Sort both the arrays
        return array1.every(function(item, index) {
            return item === array2[index];       // Check elements at every index
        });                                      // are the same
    }
    return false;
}

console.assert(compareArrays(["a", "b", "c"], ["c", "a", "b"]) === true);
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
  • You are changing the arrays as a side effect – Esailija Apr 02 '14 at 13:52
  • @Esailija Fixed it :) Please check now. – thefourtheye Apr 02 '14 at 13:55
  • Sure but why not `return array1.length === array2.length && array1.every(item => array2.indexOf(item) > -1);` much simpler and faster :P – Esailija Apr 02 '14 at 13:57
  • @Esailija I test the solutions before I post them. That is ES6 and I couldn't test that :( – thefourtheye Apr 02 '14 at 13:57
  • @Esailija Apart from that, technically that is O(N^2), I believe – thefourtheye Apr 02 '14 at 13:58
  • Yes but that doesn't mean anything in terms of real performance. I also use arrow function syntax because it's easier to read in a comment. – Esailija Apr 02 '14 at 14:04
  • @Esailija Oh, got it :) But, if there are duplicate elements in one list and the second list doesn't, this will fail, right? For example, `["a", "a"]` and `["a", "b"]`? – thefourtheye Apr 02 '14 at 14:07
  • The OP is representing sets as arrays so uniqueness is assumed yes – Esailija Apr 02 '14 at 14:17
  • @Esailija If the data is guaranteed to be unique, converting the arrays to Objects will be better, right? We get faster lookup. for larger arrays? – thefourtheye Apr 02 '14 at 14:20
  • @thefourtheye That only works if the items are all strings. I see no reason to believe that is the case. – Aaron Dufour Apr 02 '14 at 14:53
  • @AaronDufour You are correct, I don't claim that it will work always :) Also OP says `they should come as equal for the above scenario`. This solution was mainly targeted at that statement. – thefourtheye Apr 02 '14 at 14:54
  • The question here is what does it mean to have two arrays that are equal? Does it mean that the arrays have the same elements, or does it mean that the arrays indices match? – JΛYDΞV Oct 19 '22 at 10:06
0

You can try with _.difference

var diff = _(array1).difference(array2);
if(diff.length > 0) {
    // There is a difference
}

this will not work because different returns diff from first array. _.difference(['a'] ,['a','b']) is 0 but two array is not equal.

Aidin A
  • 33
  • 7
Fizer Khan
  • 88,237
  • 28
  • 143
  • 153