Intermediate Algorithm Scripting - Sorted Union

Tell us what’s happening:
Can someone explain why my code is returning an empty array. I’ve used this method before to filter out repeating elements, but it’s not working this time and I’m not sure why.

Your code so far

function uniteUnique(...arr) {
  return arr.flat()
            .filter((item, 
        index) => arr.indexOf(item) === index)
}

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

Your browser information:

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

Challenge: Intermediate Algorithm Scripting - Sorted Union

Link to the challenge:

You are very close.

function uniteUnique(...arr) {
  return arr
    .flat()
    .filter((item, index) => arr.indexOf(item) === index);
}

In which array should you be checking for the index?

Now you point it out it seems so obvious! I was checking the array of arguments and not the flattened array. Thanks for your help!

2 Likes

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