JavaScript Remove Duplicates from Array


Today we are learning how to remove duplicates from an array

28 Jul, 2020 ยท 2 min read

Today I want to share with you guys the easiest way to remove duplicates in an array with JavaScript.

We are going to be using the Set method for this.

BTW: I have a free Giveaway on Twitter! ๐Ÿšจ Free Giveaway

JavaScript Removing Duplicates from Array

We already had a full overview of the Set function before, but today we are using Set to remove duplicates from an array.

Let's start with the following array:

const array = ['๐ŸคŸ', '๐ŸคŸ', 1, 'abc', '๐ŸคŸ', 1];

As you can see, we have the emoji three times and the number one twice.

So make this a unique non-duplicate array we simply call Set on it.

const array = ['๐ŸคŸ', '๐ŸคŸ', 1, 'abc', '๐ŸคŸ', 1];
const set = new Set(array);
// Set(3)ย {"๐ŸคŸ", 1, "abc"}

Wow, awesome and simple right!

JavaScript is not always complicated loops and functions; we have to leverage the write commands for the right solution.

Feel free to have a go on this Codepen.

See the Pen JavaScript Remove Duplicates from Array by Chris Bongers (@rebelchris) on CodePen.

Browser Support

Be aware; Set is not supported in the older IE versions!

JavaScript Set support

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Spread the knowledge with fellow developers on Twitter
Tweet this tip
Powered by Webmentions - Learn more

Read next ๐Ÿ“–

TypeORM viewEntity

30 Mar, 2022 ยท 5 min read

TypeORM viewEntity

The Record Utility Type in TypeScript

12 Mar, 2022 ยท 3 min read

The Record Utility Type in TypeScript

Join 1513 devs and subscribe to my newsletter