Skip to content

Instantly share code, notes, and snippets.

@angle943
Created February 3, 2020 05:06
Show Gist options
  • Save angle943/9409576aa2b7a95043c2e6a5b3ff9ca6 to your computer and use it in GitHub Desktop.
Save angle943/9409576aa2b7a95043c2e6a5b3ff9ca6 to your computer and use it in GitHub Desktop.
Javascript Freecodecamp Algorithm #27: Find The Symmetric Difference (N Squared Solution)
const symOfTwo = (arr1, arr2) => {
const set1 = new Set(arr1);
const set2 = new Set(arr2);
const combinedArr = [...set1, ...set2];
const elObj = {};
for (const el of combinedArr) {
if (el in elObj) {
elObj[el]++;
} else {
elObj[el] = 1;
}
}
const output = [];
for (const el in elObj) {
if (elObj[el] === 1) {
output.push(Number(el));
}
}
return output;
};
function sym() {
const arrOfArrs = [...arguments];
let output = arrOfArrs[0];
for (let i=1; i < arrOfArrs.length; i++) {
output = symOfTwo(output, arrOfArrs[i]);
}
return output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment