Created
May 9, 2021 01:45
-
-
Save amrsmind/77ee9ea05cecf22187b29317fec582e5 to your computer and use it in GitHub Desktop.
Find the Symmetric Difference The mathematical term symmetric difference (△ or ⊕) of two sets is the set of elements which are in either of the two sets but not in both. For example, for sets A = {1, 2, 3} and B = {2, 3, 4}, A △ B = {1, 4}. Symmetric difference is a binary operation, which means it operates on only two elements. So to evaluate a…
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
function sym() { | |
//return symmetric difference for two arrays | |
const getSym = (x,y) => { | |
x = [...new Set(x)].sort((a, b) => a - b); | |
y = [...new Set(y)].sort((a, b) => a - b); | |
const xLength = x.length; | |
const yLength = y.length; | |
let i = 0; | |
let j = 0; | |
let result = []; | |
while(i != xLength && j != yLength){ | |
if(x[i]>y[j]){ | |
result.push(y[j]); | |
j++; | |
} | |
else if(x[i]<y[j]){ | |
result.push(x[i]); | |
i++; | |
} | |
else { | |
i++; | |
j++; | |
} | |
} | |
while(i<xLength){ | |
result.push(x[i++]); | |
} | |
while(j<yLength){ | |
result.push(y[j++]); | |
} | |
return result; | |
} | |
const argsLeng = arguments.length; | |
let tempResult = arguments[0]; | |
for(let i=1;i<argsLeng;i++){ | |
tempResult = getSym(tempResult,arguments[i]); | |
} | |
return tempResult; | |
} | |
console.log(sym([1, 2, 5], [2, 3, 5], [3, 4, 5])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment