Files
30-seconds-of-code/snippets/symmetricDifference.md
浪浪浪 61d3e3a29e Update symmetricDifference
No const
One time new Set()
One line code style
2018-08-10 15:47:20 +08:00

401 B

symmetricDifference

Returns the symmetric difference between two arrays.

Create a Set from each array, then use Array.filter() on each of them to only keep values not contained in the other.

const symmetricDifference = (a, b) => [
  ...new Set([...a.filter(v => !b.includes(v)), ...b.filter(v => !a.includes(v))])
];
symmetricDifference([1, 2, 3], [1, 2, 4]); // [3,4]