Symmetric Difference (review please)

Any suggestions for improvements.

function sym(args){
   let argArr = [...arguments]; //Array of all the arguments. 2D array.
   //A While loop that will run until there is only 1 array left i.e. answering array.
   while (argArr.length > 1){
      let symArr = []; //An empty array to store the symmetric difference between 2 sets.
      
      //Loop through first set (index 0 of arguments array).
      //Conditions: each element of first set is not present in second set and also not in the resulting symmetric difference set (to avoid repetition).
      for (let i = 0; i < argArr[0].length; i++){
         if (!argArr[1].includes(argArr[0][i]) && !symArr.includes(argArr[0][i])){
            symArr.push(argArr[0][i]);
         }
      }
      
      //Loop through second set (index 1 of arguments array).
      //Conditions: each element of second set is not present in first set and also not in the resulting symmetric difference set (to avoid repetition).
      for (let i = 0; i < argArr[1].length; i++){
         if (!argArr[0].includes(argArr[1][i]) && !symArr.includes(argArr[1][i])){
            symArr.push(argArr[1][i]);
         }
      }
      //Now that we have the symmetric difference between 2 sets, replace it with the original 2 sets in the arguments array. Arguments array will get updated and while loop will continue until there is only 1 array left in the arguments array.
      argArr.splice(0, 2, symArr);
   }
   //Convert the 2D array into 1D, sort the elements of resulting array in ascending order.
   let result = argArr[0].sort((a, b) => a - b);
   return result;
}

// Tests
console.log(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]));
console.log(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]));

The most important thing is that the solution works, so congrats on that.

Was the verbose commenting part of the requirements for the challenge? Feeling the need to put all of those comments in there is sometimes an indicator that your code is not clear enough or overly complex.

First change I made was to change the function definition to

function sym(...args){

Now maybe you weren’t allowed to do this, but it saves you having to create a whole new variable just to get these in an array.

Next change I made was to take advantage of JS’s built in Set object, since we are dealing with sets here. I converted each array to a Set which automatically removes all the duplicates. This seems a natural fit to me and relieves you of the job of having to weed out the duplicates.

Another change I made was to have a “global” object which keeps track of how many times we see each number. After you have gone through all the sets you can just go through this object once and find all of the numbers that have been seen an odd number of times and that’s your return array.

I’m sure there are improvements that can be made to mine but I feel like mine might be a little more self-explanatory and thus doesn’t need a lot of comments.

function sym(...args){
    const totals = {};
    
    args.forEach(arr => {
        const set = new Set(arr);
        set.forEach(num => {
            totals[num] = totals[num] ? totals[num]+1 : 1;
        });
    });
    
    const returnArray = [];
    for (const num in totals) {
        if (totals[num] % 2 === 1) {
            returnArray.push(Number(num));
        }
    }

    return returnArray;
}

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.