JavaScript Algorithm: Sorted Union

We write a function that will return an array of unique values taken from two or more other arrays.

Max N
The Startup

--

We write a function called uniteUnique that will accept one required argument which is an array (arr). There will be at least one or more additional array arguments.

You are given two or more arrays all containing integers. The goal of the function is to form a new array of unique values in the order they came in from their original arrays (not sorted in numerical order). The final array should contain no duplicates.

Example:

let arr = [1, 3, 2];
argument[1] = [5, 2, 1, 4];
argument[2] = [2, 1];
// output: [1, 3, 2, 5, 4]

In our example above, you are given three arrays. If we mash all the arrays together in order we get:

[1, 3, 2, 5, 2, 1, 4, 2, 1]

We highlight all the unique values in that array:

[1, 3, 2, 5, 2, 1, 4, 2, 1]

All of the highlighted unique values are taken to a new array in the order they came in from the array above.

As a result, the function returns [1, 3, 2, 5, 4].

--

--

Max N
The Startup

A writer that writes about JavaScript and Python to beginners. If you find my articles helpful, feel free to follow.