freeCodeCamp/Algorithms/symmetricDifference.js

22 lines
526 B
JavaScript

// https://www.freecodecamp.org/learn/coding-interview-prep/algorithms/find-the-symmetric-difference
function sym() {
const symDiff = {};
for (let [_, arr] of Object.entries(arguments)) {
const set = {};
arr.forEach((e) => {
set[e] = true;
});
for (let [e, _] of Object.entries(set)) {
if (symDiff[e]) {
delete symDiff[e];
} else {
symDiff[e] = true;
}
}
}
return Object.keys(symDiff).map((e) => Number(e));
}
console.log(sym([1, 2, 3, 3], [5, 2, 1, 4]));