From 063d89f0d85cfbdba4fac182f2af6a4ead7af47c Mon Sep 17 00:00:00 2001 From: Manish Date: Tue, 22 Aug 2023 10:49:10 +1000 Subject: [PATCH] Algorithms: Symmetric Difference --- Algorithms/symmetricDifference.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Algorithms/symmetricDifference.js 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]));