Is there a way to return the difference between two arrays in JavaScript?
For example:
var a1 = ['a', 'b'];
var a2 = ['a', 'b', 'c', 'd'];
// need ["c", "d"]
Any advice greatly appreciated.
Is there a way to return the difference between two arrays in JavaScript?
For example:
var a1 = ['a', 'b'];
var a2 = ['a', 'b', 'c', 'd'];
// need ["c", "d"]
Any advice greatly appreciated.
I assume you are comparing a normal array. If not, you need to change the for loop to a for .. in loop.
A better solution, if you don't care about backward compatibility, is using filter. But still, this solution works.
The above answer by Joshaven Potter is great. But it returns elements in array B that are not in array C, but not the other way around. For example, if
var a=[1,2,3,4,5,6].diff( [3,4,5,7]);
then it will output: ==>[1,2,6]
, but not[1,2,6,7]
, which is the actual difference between the two. You can still use Potter's code above but simply redo the comparison once backwards too:This should output:
[ 1, 2, 6, 7 ]
Plain JavaScript
There are two possible intepretations for "difference". I'll let you choose which one you want. Say you have:
If you want to get
['a']
, use this function:If you want to get
['a', 'c']
(all elements contained in eithera1
ora2
, but not both -- the so-called symmetric difference), use this function:Lodash / Underscore
If you are using lodash, you can use
_.difference(a1, a2)
(case 1 above) or_.xor(a1, a2)
(case 2).If you are using Underscore.js, you can use the
_.difference(a1, a2)
function for case 1.ES6 Set, for very large arrays
The code above works on all browsers. However, for large arrays of more than about 10,000 items, it becomes quite slow, because it has O(n²) complexity. On many modern browsers, we can take advantage of the ES6
Set
object to speed things up. Lodash automatically usesSet
when it's available. If you are not using lodash, use the following implementation, inspired by Axel Rauschmayer's blog post:Notes
The behavior for all examples may be surprising or non-obvious if you care about -0, +0, NaN or sparse arrays. (For most uses, this doesn't matter.)
This was inspired by the accepted answer by Thinker, but Thinker's answer seems to assume the arrays are sets. It falls apart if the arrays are
[ "1", "2" ]
and[ "1", "1", "2", "2" ]
The difference between those arrays is
[ "1", "2" ]
. The following solution is O(n*n), so not ideal, but if you have big arrays, it has memory advantages over Thinker's solution as well.If you're dealing with sets in the first place, Thinker's solution is definitely better. If you have a newer version of Javascript with access to filters, you should use those as well. This is only for those who aren't dealing with sets and are using an older version of JavaScript (for whatever reason)...
This is working: basically merge the two arrays, look for the duplicates and push what is not duplicated into a new array which is the difference.
There is a better way using ES7:
Intersection
For
[1,2,3] [2,3]
it will yield[2,3]
. On the other hand, for[1,2,3] [2,3,5]
will return the same thing.Difference
For
[1,2,3] [2,3]
it will yield[1]
. On the other hand, for[1,2,3] [2,3,5]
will return the same thing.For a symmetric difference, you can do:
This way, you will get an array containing all the elements of arr1 that are not in arr2 and vice-versa
As @Joshaven Potter pointed out on his answer, you can add this to Array.prototype so it can be used like this: