DEV Community

Manav Misra
Manav Misra

Posted on

Remove Duplicates In Array

Given an array: const myArr = [1, 2, 3, 4, 1, 1, 4], how to remove duplicates?

We can take advantage of Set - it's a built-in function constructor in JS.

const mySet = new Set(myArr)

This solves our immediate issue of removing the duplicates; now, to just turn this back into an array.

const myNewArr = [...mySet]

We have taken mySet 👆🏽 and spread it out with .... Then, we have just wrapped up these 'loose elements' into a new array as we see from the presence of [].

And, putting it all together, we can create a 'one-line utility function:' const removeDuplicates = (a) => [...new Set(a)]

You can see some other of the utility functions I routinely use here.

Top comments (4)

Collapse
 
juliandierkes profile image
Julian Dierkes

Cool haven't thought of using "Set" yet. You could also filter directly on the array:

const unique = myArr.filter((value, index, array) => array.indexOf(value) === index);

Collapse
 
miketalbot profile image
Mike Talbot ⭐

That is significantly slower though, the Set uses an O(1) lookup so the routine is O(N) whereas indexOf would make it O(N**2)

Collapse
 
juliandierkes profile image
Julian Dierkes

Nice to know. Will try that next time.

Collapse
 
miketalbot profile image
Mike Talbot ⭐

It is working in this case, those objects are each unique. It isn't working by comparing the contents of the object for sure.