# Symmetric Difference; 3 questions

Symmetric Difference; 3 questions
0

#1

3 questions

1. For set sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]) should return [1, 4, 5].; Why is 5 in it? I do understand that 1 and 4 are rather unique in their sets

2. Is there a way to run through all your code and see exactly what is happening per phase with example values? I could do console.log everywhere but I remember a tool that really showed what was happening? Because;

3. I have the idea that the result of â€śuniqueValsâ€ť in the first example is [3,5,4]. Therefore, I donâ€™t understand that reducing the â€śtotalâ€ť argsArray with something that in my eyes is the final result does give the final right result (in my eyes it should give the opposite or in other words; reducing the full array with the wrong values should give the right result). Apparently I donâ€™t understand reducing good enoughâ€¦

Feeling me ?

``````function sym(args) {
var argsArray = [].slice.call(arguments);
function symDiff(arr1,arr2){
var uniqueVals = [];
for(var i = 0; i<arr1.length; i++){
if(arr2.indexOf(arr1[i])<0 && uniqueVals.indexOf(arr1[i])<0){
uniqueVals.push(arr1[i]);
}

}

arr2.forEach(function(item){
if(arr1.indexOf(item)<0 && uniqueVals.indexOf(item)<0){
uniqueVals.push(item);
}
});
return uniqueVals;
}
return argsArray.reduce(symDiff);
}

sym([1, 2, 3], [5, 2, 1, 4]);
``````

Your Browser User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36`.

#2

Since you posted a working solution, I have blurred it out so others searching for help on this challenge do not see the solution when first viewing the topic.

To get the symmetric difference when you have 3 groups of items, you can figure out the symmetric difference between the first two and use that result to figure out the difference between this result set and the third group of items.

So in the example above, the symmetric difference between [1, 1, 2, 5] and [2, 2, 3, 5] is [1, 3]. Next we we get the symmetric difference be [1, 3] and [ 3, 4, 5, 5] which is [1, 4, 5].

#3

All right thanks and sorry for posting the solution

Now I understand what is neededâ€¦ But I wonder how the arrays are running through the codeâ€¦

Hereâ€™s my try;

• Create one array of all arguments
• Reduce this array by running a function SymDiff;
• A function that takes a first and a second argument and puts the unique numbers in uniqueVals
• ? The function then picks uniqueVals and the third argument available?
• ? The result is [1,4,5] and it reduces the full array with that but still has [1,4,5] as a result?

#4

The reduce function sends pairs of subarrays of argsArray to the symDiff function. The reduce function iterates over the array and uses the symDiff as itâ€™s callback function which has two arguments. The first argument (arr1) is the accumulator which holds the value returned each time in return uniqueVals; The second argument (curr2) is the current subarray array in argsArray in the iteration. Because you did not specify an optional initial value parameter to the reduce, then in the first iteration, it actually uses the first two subarrays for arr1 and arr2.

So, if we use the following as an example of 3 arrays being passed to sym, then in the first iteration of the reduce function, arr1 = [1, 2, 3] and arr2 = [5, 2, 1, 4]. At the end of the first iteration uniqueVals (which is [3, 5, 4] is returned, so in the second iteration of the reduce, arr1 =s [3, 5, 4] and arr2 = [1, 6].

#5

Weirdâ€¦ So simple actually Thank you so much!!!