DEV Community

Cover image for JavaScript: Best way to remove duplicates in JS Array
Kristiyan Velkov
Kristiyan Velkov

Posted on • Edited on

3 1 1 1 1

JavaScript: Best way to remove duplicates in JS Array

Hello, folks!

1) Remove duplicates from an array using a Set

A Set is a collection of unique values. To remove duplicates from an array:

  • First, convert an array of duplicates to a Set. The new Set will implicitly remove duplicate elements.
  • Then, convert the set back to an array.
  • The following example uses a Set to remove duplicates from an array:
let dublicates = ['A', 'A', 'A', 'B', 'C', 'C'];
let uniqueChars = [...new Set(dublicates)];

Output:

[ 'A', 'B', 'C' ]

Enter fullscreen mode Exit fullscreen mode

Image description

linkedin


Image description

If you like my work and want to support me to work hard, please donate via:

Revolut website payment or use the QR code above.

Thanks a bunch for supporting me! It means a LOT 😍

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

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

Okay