2023-09-18

 18 Sep 2023

Write a Function that Returns Unique Values from Arrays in the order of Insertion

Taken from FreeCodeCamp

Write a function that takes two or more arrays and returns a new array of unique values in the order of the original provided arrays.

In other words, all values present from all arrays should be included in their original order, but with no duplicates in the final array.

The unique numbers should be sorted by their original order, but the final array should not be sorted in numerical order.

Solution

This is the answer I came up with:

function uniteUnique(...arr: any): any {
    let workingArr = arr.flat(Infinity)

    let unique = new Set(workingArr)

    return Array.from(unique)
}

Testing

Solution tested in REPL:

https://www.typescriptlang.org/play?#code/GYVwdgxgLglg9mABOGUCmBVMMCOI0AUAdCQIYBO5AXIqWAJ4CUNd9iA3gFCI+IA2aKIgDuccgGsYYAOYBBSogC8tSkWB9SUAgEkwwKaiaduvAUJR40SxGDTDEAZUEFREqXMqNjvROUEhyJHlyUno1cjgAWwILfC8AX2MIBABnOAEiPjhpAhMeFHQsXHwCAG0ARgAaRABmaoAmAF1q0oBWBuqqxAAWZsRS+s7GxmqAelH+rrrEQcR2nsa85GxC7EsyrtmavraOxHLhsYmKvenWxZ8CzDWSk5nq7Zb52a7elpedgDZqgHZqgA5DohxpNTtV5t1qt9EH9EICllcius7tMmk9qm9+vNPkCQSi9hCoRdeIiboR8fdajtnp0MTsPoxgccpgS6ZwvEA

Copyright © Paramdeo Singh · All Rights Reserved · Built with Jekyll

This node last updated November 7, 2023 and is permanently morphing...

Paramdeo Singh Guyana

Generalist. Edgerunner. Riding the wave of consciousness in this treacherous mortal sea.

Technology Design Strategy Literature Personal Blogs
Search Site

Results are from Blog, Link Dumps, and #99Problems