Symmetric difference: how solution from hint works without additional removing duplicates?

So my solution for this uses helper function to remove duplicates inside one array.
Without such function I am getting results like:

[1, 1, 5, 4]//instead of expected [1, 5, 4]

I am confused by solution 3 from the hint page.

They are using diff() helper function, it doesn’t remove duplicates inside one array. I am using somewhat similar function in my approach. Below couple of test cases to show what I mean

const diffFromHint = (arr1, arr2) => [
  ...arr1.filter(e => !arr2.includes(e)),
  ...arr2.filter(e => !arr1.includes(e))
];

console.log(diffFromHint([1, 2, 3], [5, 2, 1, 4]));//[ 3, 5, 4 ]
console.log(diffFromHint([1, 2, 3, 3], [5, 2, 1, 4]));//[3, 3, 5, 4]

Yet somehow the whole solution 3 is working just fine and passing the tests. I guess I don’t know much about sets, but I can’t figure out what exactly I am missing here…

When you define a set, it will automatically rid itself of duplicate values.

Example:

const arr = [ 1, 1, 2, 3, 4, 4, 4, 5, 6, 6];
const mySet = new Set(arr);
const setArr = [ ...mySet ];

console.log(setArr); // [1, 2, 3, 4, 5, 6]
1 Like

Well, that’s extremely useful! BTW your earlier advice to look more often into the hint solutions is also appears to be useful.