Difference
JS Functions
Easy
10 min
arrays
sets
lodash
Implement difference(array, ...others). It returns a new array with the elements of array that are not present in any of the others arrays. Order is taken from array.
difference([1, 2, 3, 4], [2, 4]); // [1, 3]
difference([1, 2, 3, 4, 5], [1], [5, 6]); // [2, 3, 4]
difference(['a', 'b'], []); // ['a', 'b']
difference([NaN, 1], [NaN]); // [1]
difference([1, 1, 2], [2]); // [1, 1]
Requirements
- Accept any number of exclusion arrays (zero or more).
- Compare with SameValueZero (the rule
Setandincludesuse):NaNmatchesNaN, and0matches-0. Objects match only by reference. - Duplicates in
arraythat are not excluded are kept (this is not a set operation onarrayitself). - Return a new array. Don't mutate any argument.
- Aim for better than O(n × m) time.
Follow-up
- How would you add a
differenceBy(array, values, iteratee)that compares by a derived key, such asid? - When is the naive
includesversion actually faster than building aSet?
Spotted a mistake or a rights concern? Report an issue