Here’s the link to the challenge.
It took me 2 hours to find a solution, and I would like to have my code reviewed for improvements on readability and simplification.
Thanks!
function sym(args) {
// Creating an array from the arguments object
var arr = Array.from(arguments);
// I figured that it would be the easiest to use an sepperate array for the first set to be tested
var arrCheck1 = arr.shift();
//this array acts as a receiver for the symmetric difference
var arrCheck2 = [];
//this function filters the symetric difference from "arrCheck1"
function reduceCheck(accumulator, element){
if (arr[0].indexOf(element) == -1 && accumulator.indexOf(element) == -1){
accumulator.push(element);
}
return accumulator;
}
// this function filters the symetric difference from the 1st element of "arr"
function reduceArr(accumulator, element){
if (arrCheck1.indexOf(element) == -1 && accumulator.indexOf(element) == -1){
accumulator.push(element);
}
return accumulator;
}
// This loop loops through "arr", assigning the symmetric difference it has with "arrCheck1" to "arrCheck2", and if there are more than 2 sets in "arr" to be filtered, it assigns the symmetric difference to "arrCheck1" and removes the 1st element from "arr"
while (arr.length > 0){
arrCheck2 = arrCheck1.reduce(reduceCheck, []).concat(arr[0].reduce(reduceArr, []));
arrCheck1 = arrCheck2;
arr.shift();
}
return arrCheck2;
}