This post will discuss how to remove duplicate values from an array in JavaScript.

1. Using Array.filter() function

A simple solution is to check the second occurrence for each array element. This can be easily done using the indexOf() method with the filter() method in the following manner:

Download  Run Code

2. Using Set

The above solution is not recommended because of serious performance concerns for large arrays. An efficient solution is to convert the array into a Set data structure and then transform the set back into an array. This solution works as a Set store unique values and eliminates duplicate values. Note that this solution might change the original ordering of elements present in the array. This can be implemented using the Set constructor and Array.from() method.

Download  Run Code

 
Alternatively, you can use the Spread operator (...) to convert the set back into an array.

Download  Run Code

3. Using Underscore/Lodash Library

In case you don’t want to use the Set data structure, Underscore, or Lodash JavaScript library provides the uniq() method, which returns a duplicate-free version of an array.

Download Code

4. Using jQuery

If you’re using the jQuery library, consider using the grep() method, which works similarly to the JavaScript filter() method. The following example demonstrates its usage:

Download Code

5. Using Array.reduce() function

Finally, you can use the reduce() method in the following manner to remove duplicates from an array:

Download  Run Code

That’s all about removing duplicates from an array in JavaScript.