function uniteUnique(...arrays) {
//make an array out of the given arrays and flatten it (using the spread operator)
const flatArray = [].concat(...arrays);
// create a Set which clears any duplicates since it's a regular set and not a multiset
return [...new Set(flatArray)];
}
// test here
uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]);
Yes, the array is not sorted because at first there is a concatenation of arrays into one array and result is
[1, 3, 2, 5, 2, 1, 4, 2, 1]
And then we are calling the new Set() method which remove all duplicate occurrences but considering the first occurrence to be valid and the others duplicate occurrences of the same element will be removed thus NOT SORTING
[1, 3, 2, 5, 4]
The result is not sorted because each number is put from the first occurrence point (index) and the other similar number(s) at a later point (index) are removed.
We can use the sort method to sort the result if need be.