DEV Community

Manav Misra
Manav Misra

Posted on

3

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.

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay