Find the Symmetric Difference help

Find the Symmetric Difference help
0

#1

Hi,

My solution is working but I would like to know if it is a viable solution or it is too complicated or it is just wrong as it is and should find a better solution.

Your code so far


function sym(args) {
  var set = new Set();
  var setNext = new Set();
  var l = arguments.length;

  arguments[0].forEach(elem => set.add(elem));

  for (var i = 1; i < l; i++) {
    arguments[i].forEach(elem => {
      setNext.add(elem);
    });
    setNext.forEach(elem => {
      if (set.has(elem)) {
        set.delete(elem);
      } else set.add(elem);
    })
    setNext.clear();
  }

  return Array.from(set);

}

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

Your browser information:

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

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


#2

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution.

It is a viable solution and it is not wrong because it works. When you start asking if is it too complicated or if you should find a better solution, your are inviting others to just start posting their “best” solution. For now, just accept that it works. You can always try writing other solutions to the problem and compare their performance if you want.


#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;
}

Your solution is totally working and viable. I don’t say I came up with a better solution, but I think mine is a simpler and more understandable.