Skip to content

Instantly share code, notes, and snippets.

@ronihcohen
Last active August 29, 2015 14:25
Show Gist options
  • Save ronihcohen/17667611bdc09c5ebe00 to your computer and use it in GitHub Desktop.
Save ronihcohen/17667611bdc09c5ebe00 to your computer and use it in GitHub Desktop.
function sym() {
var args = Array.prototype.slice.call(arguments);
var flattened;
if (args.length>1){
flattened = args.reduce(function(previousValue, currentValue) {
var pre = clearIntersection (previousValue,true);
var cur = clearIntersection (currentValue,true);
var res = clearIntersection(pre.concat(cur));
console.log(pre+"-"+cur+"="+res);
return res;
});
} else {
flattened = clearIntersection (args[0],true);
}
function clearIntersection(flattened,saveOne){
for (var outi = 0; outi < flattened.length; outi++){
for (var ini = 0; ini < flattened.length; ini++){
if (outi!==ini && flattened[outi]===flattened[ini]){
if (saveOne){
flattened[outi] = null;
} else{
flattened[outi]=flattened[ini] = null;
}
}
}
}
return cleanNull(flattened);
}
function cleanNull(flattened){
for (var cleanI = 0; cleanI < flattened.length; cleanI++){
if (flattened[cleanI]===null){
flattened.splice(cleanI, 1);
cleanI--;
}
}
return flattened;
}
return flattened;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment