# Solution not sorted

One solution is:

``````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]);
``````

But I tried this and it’s not sorted.

``````function uniteUnique(...arr) {
const flatArray = [].concat(...arr);
console.log(flatArray)
console.log([...new Set(flatArray)]);
return [...new Set(flatArray)];
}

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

It isn’t sorted in numerical order, but it is sorted by the order in which each number appears in the original arrays, as per description.

1 Like

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.

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