How To Remove Duplicates From An Array In JavaScript

Remove Duplicates from an Array in JavaScript

There is no built-in way to remove duplicates from an array in JavaScript. To achieve the goal, a custom function must be created. We will learn how to solve this problem using two different methods. Both methods are rather straightforward, and you can choose whichever suits you most.

Remove duplicates from an array in JavaScript

Method 1: Remove duplicates from an array in JavaScript using for loop

The includes() function returns true if an array already contains an element with the same value. Otherwise, it returns false. The following example uses a for loop to visit all the elements of an array and only add to the new array such element which does not exist in the new array:

let arr = ['r','E','m','o','v','E',' ','d','u','p','l','i','c','a','t','E'];
let res = [];

for (char of arr)
    if (res.includes(char) == false)
         res.push(char);

console.log(arr.toString());
console.log(res.toString());

Syntax:

includes(searchElement)

Parameter:

  • searchElement: The value to search for.

Output:

r,E,m,o,v,E, ,d,u,p,l,i,c,a,t,E
r,E,m,o,v, ,d,u,p,l,i,c,a,t

The logic behind this method is straightforward. We are simply using the loop to iterate through all the elements and then add to a new array only values that do not appear there:

Method 2: Remove duplicates from an array in JavaScript using set()

A Set is a collection of elements that only appear once. Hence no value is repeated in the set. We want to achieve an array without duplicates, which works like a set, and from there we will have to convert the array into a new set. And here is how to do it:

let arr = ['r','E','m','o','v','E',' ','d','u','p','l','i','c','a','t','E'];
let res = [...new Set(arr)];

console.log(arr.toString());
console.log(res.toString());

The three dots operator (also called the spread operator) can convert a set object into array elements in JavaScript.

The method Set() can only be constructed with new. Attempting to call it without new throws a TypeError.

Syntax:

new Set(it)

Parameter:

  • it: All of an iterable object’s elements will be included in the new Set if this parameter is passed.

Output:

r,E,m,o,v,E, ,d,u,p,l,i,c,a,t,E
r,E,m,o,v, ,d,u,p,l,i,c,a,t

Using the Set() method can help you remove duplicates from an array in just a line of code. Moreover, this method is well-known for its effective complexity, which means it takes the least time to implement compared to other approaches. Therefore, we highly recommend you use the Set() constructor to remove duplicates from an array in JavaScript.

Summary

We have learned how to remove duplicates from an array in JavaScript using two different methods. Although we have many ways to cope with this type of problem, we advise you to use the method of using Set() constructor because it is the most efficient method.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *