Skip to content

Instantly share code, notes, and snippets.

@rfprod
Last active April 22, 2017 15:52
Show Gist options
  • Save rfprod/94b8aac9b1f416ca9960 to your computer and use it in GitHub Desktop.
Save rfprod/94b8aac9b1f416ca9960 to your computer and use it in GitHub Desktop.
Symmetric Difference
function sym(args) {
args = Array.prototype.slice.call(arguments);
var symDif = [];
for (var t=0;t<args.length;t++){
switch (t){
case 0:
if (args[t+1]){rdcVSargs(t);}
break;
case 1:
break;
default:
if (args[t]){rdcVSsymDif(t);}
break;
}
}
function removeSameNumbersInArray(arr){
arr.reduce(function(previousValue,currentValue,currentIndex,array){
if (previousValue == currentValue){arr.splice(currentIndex-1,1);}
});
}
function rdcVSargs(i){
removeSameNumbersInArray(args[i]);
args[i].reduce(function(previousValue,currentValue,currentIndex,array){
if (previousValue && args[i+1].indexOf(previousValue) == -1){
symDif.push(previousValue);
}
if (args[i+1].indexOf(currentValue) == -1){
symDif.push(currentValue);
}
});
removeSameNumbersInArray(args[i+1]);
args[i+1].reduce(function(previousValue,currentValue,currentIndex,array){
if (previousValue && args[i].indexOf(previousValue) == -1){
symDif.push(previousValue);
}
if (args[i].indexOf(currentValue) == -1){
symDif.push(currentValue);
}
});
}
function rdcVSsymDif(i){
removeSameNumbersInArray(symDif);
var removedArgsValues = [];
symDif.reduce(function(previousValue,currentValue,currentIndex,array){
for (var o=0;o<args[i].length;o++){
if (previousValue && previousValue === args[i][o]){
console.log("removing value: "+(currentValue-1));
symDif.splice(currentIndex-1,1);
console.log("symDif: "+symDif);
removedArgsValues.push(args[i][o]);
}
if (currentValue === args[i][o]){
console.log("removing value: "+currentValue);
symDif.splice(currentIndex,1);
console.log("symDif: "+symDif);
removedArgsValues.push(args[i][o]);
}
}
return symDif;
});
console.log("removedArgsValues: "+JSON.stringify(removedArgsValues));
removeSameNumbersInArray(args[i]);
console.log("symDif: "+symDif);
console.log("arguments: "+args[i]);
if (args[i].length < 2 && symDif.indexOf(args[i][0] == -1) && removedArgsValues.indexOf(args[i][0]) == -1){
console.log("adding value: "+args[i][0]);
symDif.push(args[i][0]);
console.log("symDif: "+JSON.stringify(symDif));
}
args[i].reduce(function(previousValue,currentValue,currentIndex,array){
if (currentIndex === 1 && symDif.indexOf(previousValue) == -1 && removedArgsValues.indexOf(previousValue) == -1){
console.log("adding value: "+previousValue);
symDif.push(previousValue);
console.log("symDif: "+JSON.stringify(symDif));
}
if (symDif.indexOf(currentValue) == -1 && removedArgsValues.indexOf(currentValue) == -1){
console.log("adding value: "+currentValue);
symDif.push(currentValue);
console.log("symDif: "+JSON.stringify(symDif));
}
});
}
return symDif;
}
//sym([1, 2, 3], [5, 2, 1, 4]);
//sym([1, 2, 5], [2, 3, 5], [3, 4, 5]);
//sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]);
sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]);
//sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]);

Symmetric Difference

Function takes two or more arrays and returns an array of the symmetric difference of the provided arrays - (((A∆B)∆C)∆...)∆Nn.

The mathematical term symmetric difference refers to the elements in two sets that are in either the first or second set, but not in both.

(A∆B)∆C - comprehensive explanation

A script by V.

License.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment