Practicing the basics - Find symmetric diff

Trying to practice my (now rusty) algo writing skills so this code is gonna look a little rough but I’d like to power through and re-build algo skills and knowledge rather than just read answers.

My pseudo-code for this is:

  1. Create unique arrays out of each of the args sent in.
  2. For each of those unique arrays compare to every other array to see if this array has any number that don’t exist in any of the others. If so, push it to an empty result array.

Where I’m having trouble is getting to the last array. I know it’s b/c I am limiting the for loop till k < interArray.length - 1 but of course if I don’t do that, then it will look for k[interArray.length] which doesn’t exist.

Code so far:

function sym(...args) {
  let interArray = [];
  for (let i = 0; i < args.length; i++) {
    const uniqueSet = new Set(args[i]);

    const backToArray = [...uniqueSet];
    interArray.push(backToArray);
  }

  let result = [];
  for (let k = 0; k < interArray.length - 1; k++) {
    for (let i = 0; i < interArray[k].length; i++) {
      if (interArray[k + 1].indexOf(interArray[k][i]) === -1) {
        result.push(interArray[k][i]);
      }
    }
  }

  console.log(result);

  return result;
}

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

Challenge: Find the Symmetric Difference

Link to the challenge:

You’re iterating over all of the arrays passed and then iterating over their values, right? Now, would you like to check if any of the others arrays has the value you are in or just if the next array has it? And why?

Tip: there’s a simpler and faster way to solve it with hash tables. Worth researching and thinking through it.

Explain your current algorithm (not your currently written code). Once you understand a working algorithm, then you can write the code.