I received this question for practice and the wording confused me, as I see 2 results that it might want.

And either way, I’d like to see both solutions.

For example, if I have an array:

``let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];``

I’m taking this as wanting the final result as either:

``let finalResult = [1, 2, 3, 4, 5, 8, 9, 10];``

OR:

``let finalResult = [1, 9, 10];``

The difference between the two being, one just removes any duplicate numbers and leaves the rest and the second just wants any number that isn’t a duplicate.

Either way, I’d like to write two functions that does one of each.

This, given by someone else gives my second solution.

``````let elems = {},

arr2 = arr.filter(function (e) {
if (elems[e] === undefined) {
elems[e] = true;
return true;
}
return false;
});
console.log(arr2);``````

I’m not sure about a function for the first one (remove all duplicates).

Using Set and Array.from()

``````let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];

console.log(Array.from(new Set(arr)));``````

Run code snippetExpand snippet

Alternate using regex

regex explanation here

``````let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];

let res = arr
.join(',')
.replace(/(\b,\w+\b)(?=.*\1)/ig, '')
.split(',')
.map(Number);

console.log(res);``````

Run code snippetExpand snippet

Alternate using objects

``````let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];

let obj = arr.reduce((acc, val) => Object.assign(acc, {
[val]: val
}), {});

console.log(Object.values(obj));``````