DEV Community

Ishaan Bedi
Ishaan Bedi

Posted on • Originally published at blog.ishaanbedi.me on

Remove Duplicate Elements From An Array With Just a Single Line Of Code!

In today's article, we'll be looking at a super simple trick that can be used to remove duplicate elements from an array in Javascript.

With the help of this trick, you can delete the duplicate elements from an array without the use of loops or other complex operations.

We'll be using the concept of Sets in Javascript to achieve this.

So let's start...

We start with initializing a new example array, on which we’ll perform the operation to remove duplicate elements.

var arr = [1,2,2,3,3,3,4,4,4,4,5,5,5,5,5];

Enter fullscreen mode Exit fullscreen mode

In this example array, we have duplicate integers (as many times as their value). We want to remove duplicate elements from this array and our updated array should look like this:

[1, 2, 3, 4, 5]

Enter fullscreen mode Exit fullscreen mode

Now to remove duplicate elements, the simplest part, we use a combination of arrays’ spread operator (to spread the elements of the array) along with creating a new Set which will allow us to store only the unique elements from the array.

        arr = [... new Set(arr)];

Enter fullscreen mode Exit fullscreen mode

This re-initializes the variable arr with the non-duplicate elements, just like the way we want.

Arr -> [1, 2, 3, 4, 5]

Enter fullscreen mode Exit fullscreen mode

If you want to retain the original array, you may declare a new variable that stores the non-duplicate elements array from the original array.

Ex.

var arr = [1, 2, 2, 3, 3, 3];
var newArr = [... new Set(arr)];

Enter fullscreen mode Exit fullscreen mode

Through this way, we get a new array with no duplicate elements, and we also have our original array, which we might need later.

Arr -> [1, 2, 2, 3, 3, 3]

newArr -> [1, 2, 3]

Not only integers, but we can use this concept for any data type. Let’s take an example with elements of type String.

var stringArray = ['a', 'b', 'b', 'c', 'd', 'd', 'd', 'e', 'e', 'f'];

Enter fullscreen mode Exit fullscreen mode

Now, using the same concept as discussed above, we use

stringArray = [... new Set(stringArray)];

Enter fullscreen mode Exit fullscreen mode

And, as you guessed, the output after removing all the duplicate elements from the array is


["a", "b", "c", "d", "e", "f"]

Enter fullscreen mode Exit fullscreen mode

The last example we take is with an array of boolean values.

var boolArray = [true,true,false,false,true,false];

Enter fullscreen mode Exit fullscreen mode

Applying the same concept, we use:

boolArray = [... new Set(boolArray)];

Enter fullscreen mode Exit fullscreen mode

And as expected, we have the boolArray with only two elements, ie,

[true, false]

Enter fullscreen mode Exit fullscreen mode

The final key point:

The Set() constructor lets us create Set Objects that lets us store the unique value of any data type.

Thank you for reading this far. Check out more at www.ishaanbedi.me

Top comments (0)