Find the Symmetric Difference Issue with Test?

Find the Symmetric Difference Issue with Test?
0

#1

Tell us what’s happening:
I’m supposed to find symmetric difference between 2 or more sets.

When given [1, 2, 5], [2, 3, 5], and [3, 4, 5], I think the answer should be [1, 4], but the test says I should return [1, 4, 5].

Maybe I don’t understand symmetric difference, but with 3 sets, if a value is in all three, shouldn’t it be excluded from the result? So why is 5 included?

Your code so far


function sym(...args) {
  const numArrays = args.length;
  let result = [];
  for (let i = 0; i < numArrays; i++) {
    for (let j = 0; j < args[i].length; j++) {
      let currentValue = args[i][j];
      let seen = 0;
      for (let k = 0; k < numArrays; k++) {
        if (args[k].includes(currentValue)) {
          seen++;
        }
      }
      if (seen == 1 && !result.includes(currentValue)) {
        result.push(currentValue);
      }
    }
  }
  return result;
}

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/coding-interview-prep/algorithms/find-the-symmetric-difference/


#2

I think I figured it out. Symmetric difference is meant to be taken in turn, not altogether.

What I mean by that is that first you calculate the symmetric difference for the first two arrays, then you calculate symmetric difference with the RESULT of the first calculation and the next array.

I was not approaching the problem correctly.


#3

Thanks for clarifying, I got stuck on sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]); // -> [2,3,4,6,7]
because the description doesn’t really explain very well what symmetrical difference is.

They should add your explanation to the problem.


#4

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[0],arguments[1]);
for(let k=2; k<arguments.length;k++){
result = delta(result,arguments[k]);
}
return result;
}