Algorithms: Symmetric Difference
This commit is contained in:
parent
6a10d5ddfa
commit
063d89f0d8
21
Algorithms/symmetricDifference.js
Normal file
21
Algorithms/symmetricDifference.js
Normal 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]));
|
Loading…
Reference in New Issue
Block a user