Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Manav Misra
Manav Misra

Posted on

     

Remove Duplicates In Array

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

We can take advantage ofSet - it's a built-infunction constructor in JS.

const mySet = new Set(myArr)

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

const myNewArr = [...mySet]

We have takenmySet 👆🏽 and spread it out with.... Then, we have just wrapped up these 'loose elements' into a newarray 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 usehere.

Top comments(4)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
juliandierkes profile image
Julian Dierkes
I am a young full stack web developer at exb and mainly working with Angular.In my free time I love making music and enjoy doing all sorts of craftsmanship.
  • Location
    Berlin
  • Education
    MSc in Electronics
  • Work
    Full Stack Developer
  • Joined

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);

CollapseExpand
 
miketalbot profile image
Mike Talbot ⭐
Serial CTO
  • Location
    Bristol, UK
  • Work
    Chief Technology Officer
  • Joined

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)

CollapseExpand
 
juliandierkes profile image
Julian Dierkes
I am a young full stack web developer at exb and mainly working with Angular.In my free time I love making music and enjoy doing all sorts of craftsmanship.
  • Location
    Berlin
  • Education
    MSc in Electronics
  • Work
    Full Stack Developer
  • Joined

Nice to know. Will try that next time.

CollapseExpand
 
miketalbot profile image
Mike Talbot ⭐
Serial CTO
  • Location
    Bristol, UK
  • Work
    Chief Technology Officer
  • Joined

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

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I'm a JS Subject Matter Expert (SME) that has spent the past few years spearheading curricula and teaching initiatives at colleges and bootcamps, in person and virtually.
  • Location
    62236
  • Education
    BS - Mech. Eng. - Missouri S&T
  • Joined

More fromManav Misra

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp