# Find the Symmetric Difference | Tests?

Tell us what’s happening:
I’ve tried my best to wrap my head around what this problem wants me to do, but I really don’t understand how some of the tests should return certain answers; for example the instructions say to my understanding
“if the number is not unique among the other arrays , return this value” .

However by this understanding the input
`[1, 2, 5], [2, 3, 5], [3, 4, 5]` should return `[1,4]` and definitely not 5, because 5 is included in every other array. What am I doing wrong?

``````
function sym(...args) {

let testArr = [];
let newArr = [];
for (var i = 0; i < args.length; i++){
args[i].map(val => {
testArr.push(val);
for (var j = 0; j < args.length; j++){
if (args[j].includes(val) && args[j] != args[i]){
newArr.push(val);
}
}

});
}
let simplified = newArr.filter((val,index) => newArr.indexOf(val) == index);
let result = testArr.filter(num => simplified.includes(num) == false);
//just to sort the numbers
result.sort((A,B) => A > B);
result = result.filter((val,index) => result.indexOf(val) == index);
console.log(result);
return result;
}

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

``````

User Agent is: `Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36`.

This what you need.

I think you’re incorrectly generalizing “symmetric difference” as it applies to more than 2 parameters.

Note, how you’re generalizing seems perfectly logical:
A△B = the values that appear in only one of A or B, but not both.
therefore
A △ B △ C = the values that appear in only one of A, B, and C, but not more than one.

However, that is not the correct definition.

The proper definition appears to be:
A △ B △ C = (A △ B ) △ C

therefore if
A = [1, 2, 5],
B = [2, 3, 5],
C = [3, 4, 5]

A △ B = [1, 3]
and
[1, 3] △ C = [1, 4, 5]

2 Likes

Hi, I would like to help you. I came up with a simpler and basic level solution. Firstly, we have to understand the problem,let us take 3 arguments(A,B,C) for clear explanation. If the symmetric difference between arrays A and B is X, then the next symmetric difference should be calculated between X and C, but not between B and C.

This process of calculation should be continued till the last argument.

In my code, I have created a function to find out the symmetric difference between two arrays. And I designed my function in a way such that it produces unique values in ascending order as result.

function delta(arr1, arr2) {
let resArr = [];
for (let i = 0; i < arr1.length; i++) {
if (!arr2.includes(arr1[i])) {
resArr.push(arr1[i]);
}
}
for (let j = 0; j < arr2.length; j++) {
if (!arr1.includes(arr2[j])) {
resArr.push(arr2[j]);
}
}
let finArr = resArr.sort(function (a, b) {
return a - b;
})
let newArr = Array.from(new Set(finArr));
return newArr;
}

As I mentioned earlier, we have to calculate the symmetric difference between first two arrays, and it is stored as result. Now, we have to calculated the SD between result and third array, that value is stored in result(result’s value wil be replaced according to my code), and again SD is calculated between result and fourth array. This process continues till the last argument, and I used my function delta to calculate the symmetric differences. My code is:

function sym(args) {
let result = delta(arguments,arguments);
for(let k=2; k<arguments.length;k++){
result = delta(result,arguments[k]);
}
return result;
}

Try to solve with out seeing the solution.