Find the Symmetric Difference

Tell us what’s happening:

Hey Guys I am trying solve this challenge the way as I see it
but i dont get there can any body help me
Thanks

Your code so far


function sym(args) {
     var i ;
     var j ;
     var k ;
     for (i = 0; i < args.length; i++) {
       for (j = 0; j < args[i].length; j++) {
         while (k < args[i].length) {
           k++;
           if (k[i] == )

         }

     }
}

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

Your browser information:

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

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

First, can you describe how you will solve the problem by hand? For example, do you know how to reach the output of sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5])?

For example, lets call each of the array above A, B, C.

  1. Compare A[0] to each element in B
  2. Write down A[0] if it doesn’t show up in B
  3. Do above for all elements in A

If you can describe your approach, then I can help with the coding.

1 Like

what I am trying to say here if you can help me complete the challenge the way I have started it above
I know how to solve it by hand but when it come to coding its a little bit harder try to solve it using different method like forEach and map but doesn’t work so i have turned out to loops pretty simple and logical so if you can explain me while complete this challenge
Thanks

I’ve said the earlier because what you’ve written so far doesn’t seem to reflect your understanding of the problem.
Anyway, here’s one method which might be what you’ve been thinking (considering all the nested for loops)…

I will call symmetric difference SD from now on. And I won’t use forEach(), map(), and etc… since you seem comfortable with for loop.

You can see handling the SD of arbitrary number of input is calling SD on two inputs successively. For example

SD([1, 2, 5], [2, 3, 5], [3, 4, 5]) => SD( SD([1, 2, 5], [2, 3, 5]), [3, 4, 5] )

So, let’s solve SD that handles two inputs first because handling all input in one function will be messier.

Here’s a psuedocode for solving SD in the most primitive way (in my opinion).

result = []
A = [...], B  = [...]

for 1..n in A as a
    if B does not include a
        add a to the result

for 1..n in B as b
    if A does not include b
        add b to the result

return unique(result) // because we didn't handle potential duplicates

Here’s a code that does that:

function sd(A, B) {
  let res = []

  for (let i = 0; i < A.length; ++i) {
    if (!B.includes(A[i])) {
      res.push(A[i])
    }
  }

  for (let i = 0; i < B.length; ++i) {
    if (!A.includes(B[i])) {
      res.push(B[i])
    }
  }

  return unique(res)
}

function unique(A) {
  let res = {}
  for (let i = 0; i < A.length; ++i) {
    res[ A[i] ] = A[i]
  }
  return Object.values(res)
}

Now you want to apply SD many times as I’ve described earlier:

SD(arg1, arg2, arg3...) =>
    res1 = SD(arg1, arg2)
    res2 = SD(res1, arg3)
    res3 = SD(res2, arg4)
    ...
    return res

Here’s a code that does that:

function sdAll(args) {
  // Assume at least 2 array arguments
  let res = sd(arguments[0], arguments[1])

  for (let i = 2; i < arguments.length; ++i) {
    res = sd(res, arguments[i])
  }

  return res
}

Notice you can’t access arguments by index by doing “args[0], args[1] …”. You have to use ‘arguments’ or ES2015 …arg syntax, or the awkward slice.call method.

So, that’s one way to compute symmetric difference. Normally, I wouldn’t post the direct answer but I honestly couldn’t figure out how to start explaining based on what you’ve wrote.

Also, there are better ways of doing this; but I find it less intuitive than this one.

Thanks alot you helped me so much
I tried doing my own solution using for loop as well I could switch over to Switch but I preferred for loop
I couldn’t reach there could you tell me please what is the problem:

function sym(args1, args2, args3) {
var result = [];
for (var i =0 ; i < args1.length; i++) {
  if (!args2.includes(args1[i])) {
    result.push(args1[i]);
  }
}
for (var j = 0;  j < args2.length; j++) {
  if (!args1.includes(args2[j])) {
    result.push(args2[j]);
  }
}
for (var n = 0; n < args3.length; n++) {
  if (!args1.includes(args3[n])) {
    result.push(args3[n]);
  }
}
for (var m = 0; m < args1.length; m++) {
  if (!args3.includes(args1[m])) {
    result.push(args1[m]);
  }
}
for (var s = 0; s < args2.length; s++) {
  if (!args3.includes(args2[s])) {
    result.push(args2[s]);
  }
}
for (var y = 0; y < args3.length; y++) {
  if (!args2.includes(args3[y])) {
    result.push(args3[y]);
  }
}

   return unique(result);
}
function unique(args1, args2, args3) {
  var result = {}
  for (var r = 0; r < args1.length; r++) {
    result[ args1[r]  ]= args1[r];
  for (var z = 0; z < args3.length; z++) {
    result[ args3[z]  ]= args3[z];
  }
  for (var l = 0; l < args2.length; l++) {
    result[ args2[l]  ]= args2[l];
  }
  
  return Object.values(result);
}
}

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

Thanks

I don’t understand why you’ve said:

switch and for loop are not related, and using switch won’t likely strengthen the code in this case.

Also, in this case you should learn how to handle a function that handles many arguments (not just a fixed number of arguments)

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

Hope, my solution helps you to understand the problem.

arguments is an Array -like object accessible inside functions that contains the values of the arguments passed to that function. eg:-

function func1(a, b, c) {
console.log(arguments[0]);
// expected output: 1

console.log(arguments[1]);
// expected output: 2

console.log(arguments[2]);
// expected output: 3
}