Sorted Union my solution

Continuing the discussion from freeCodeCamp Challenge Guide: Sorted Union:

i simply used spreed operator and a for loop. The solution is pretty basic though.

function uniteUnique(arr, …arrN) {
for (let i = 0; i < arrN.length; i++) {
for (let j = 0; j < arrN[i].length; j++) {
if (!arr.includes(arrN[i][j])) {
arr.push(arrN[i][j]);
}
}
}
return arr;
}
console.log(uniteUnique([1, 2, 3], [5, 2, 1, 4], [2, 1], [6, 7, 8]));

Hello there,

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Also, for future posts, when you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

Please use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks are not single quotes.

markdown_Forums

Thank you.

1 Like

You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.

We have set your post to unlisted. Thanks for your understanding.

Rest parameters + spread operator + array.flat() + filter = Easy and elegant solution.

function uniteUnique(...arr) {
  const array = [...arr].flat();
  const result = array.filter((a,b) => 
   array.indexOf(a) === b
   );
  return result;
}

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

1 Like