DEV Community

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

Posted on • Updated on

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 😍

Top comments (0)