Created
February 3, 2020 05:06
-
-
Save angle943/9409576aa2b7a95043c2e6a5b3ff9ca6 to your computer and use it in GitHub Desktop.
Javascript Freecodecamp Algorithm #27: Find The Symmetric Difference (N Squared Solution)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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