AlgoDaily 20: Uniqueness of Arrays

https://algodaily.com/challenges/uniqueness-of-arrays

This is pretty trivial with the Set class in ECMAScript 2015.

function uniques(items) {
        return [...new Set(items)];
}

If that seems like cheating, then we can achieve this with a hash map and a new array:

function uniques(items) {
        const seen = {};
        const unique = [];
        for (let x of items) {
                if (seen[x] === undefined) {
                        unique.push(x);
                        seen[x] = true;
                }
        }
        return unique;
}

This is O(n).


Tech mentioned