Algorithms: Symmetric Difference

This commit is contained in:
Manish 2023-08-22 10:49:10 +10:00
parent 6a10d5ddfa
commit 063d89f0d8

View File

@ -0,0 +1,21 @@
// 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]));