Files
30-seconds-of-code/snippets/symmetricDifference.md
2017-12-27 16:32:27 +02:00

17 lines
425 B
Markdown

### 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.
```js
const symmetricDifference = (a, b) => {
const sA = new Set(a), sB = new Set(b);
return [...a.filter(x => !sB.has(x)), ...b.filter(x => !sA.has(x))];
}
```
```js
symmetricDifference([1,2,3], [1,2,4]) -> [3,4]
```