diff --git a/Algorithms/symmetricDifference.js b/Algorithms/symmetricDifference.js new file mode 100644 index 0000000..93d0034 --- /dev/null +++ b/Algorithms/symmetricDifference.js @@ -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]));