Find the Symmetric Difference Issue in Test cases

Find the Symmetric Difference Issue in Test cases
0

#1

Tell us what’s happening:
I have come up with the below solution which seems to be working fine in some of the test cases.
But some test case have expected results which are not correct and hence is not accepting the solution as correct.like this one:

sym([1, 2, 5], [2, 3, 5], [3, 4, 5]) should return [1, 4, 5]

here 5 is clearly symmetrical and is available in all three arrays how it can be present in the result array? There are more suspicious test cases like this one.

Your code so far


function sym(...args) {
 const finalArray = [];
 const uniqueArray = [];
 const seenArray = [];   

    args.forEach((e)=>{
      var arr = e.filter((v, i, a) => e.indexOf(v) === i)
      uniqueArray.push(arr);
    })

    uniqueArray.forEach((e)=>{
        e.forEach((it) => {
            var index = finalArray.indexOf(it);
            if(index===-1){
                finalArray.push(it);
            }else {
                seenArray.push(it);
            }
        })
    })

    seenArray.forEach((item)=>{
       var index = finalArray.indexOf(item);
       if(index>-1){ 
        finalArray.splice(index,1);
       }
    })

  return finalArray;
}

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

Your browser information:

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

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


#2

Straight from the instructions:

… the mathematical term “symmetric difference” of two sets is the set of elements which are in either of the two sets, but not in both

implying sequential evaluation.


#3

Seems my approach was not correct, if sym([A], [B], [C]) is the function then it will be simplified to first solving [A] & [B] then result needs to be solved with [C] as a sequential evaluation. Thanks @lynxlynxlynx