Skip to content

CodeNot attempted

Symmetric difference

Difficulty: easy

Return the symmetric difference of any number of arrays, applied pairwise left to right, with no duplicates and in order of first appearance: sym([1, 2, 5], [2, 3, 5], [3, 4, 5]) → [1, 4, 5].

Show reference solution
export function sym(...arrays: number[][]): number[] {
function diff(a: number[], b: number[]): number[] {
const onlyInA = a.filter((x) => !b.includes(x));
const onlyInB = b.filter((x) => !a.includes(x));
return [...new Set([...onlyInA, ...onlyInB])]; // a value repeated in one array counts once
}
return arrays.reduce(diff, []);
}
Loading editor…