Array difference

OSCC · JavaScript, Array · Oct 19, 2020

Calculates the difference between two arrays, without filtering duplicate values.

const difference = (a, b) => {
  const s = new Set(b);
  return a.filter(x => !s.has(x));
};
difference([1, 2, 3, 3], [1, 2, 4]); // [3, 3]

Recommended snippets & collections

  • JavaScript Array Tricks

    Snippet collection

    Learn a handful of awesome tips and tricks that you can leverage in your code to make array manipulation a breeze.

  • Returns the symmetric difference between two arrays, without filtering out duplicate values.

  • Returns the unique symmetric difference between two arrays, not containing duplicate values from either array.

  • Returns the symmetric difference between two arrays, after applying the provided function to each array element of both.