How to Remove Duplicates From a JavaScript Array

When you need to remove duplicates from an array in JavaScript, consider these methods.

Written by Jayanth Somineni
Software engineer reviewing code on computer screen
Image: Shutterstock / Built In
Brand Studio Logo
UPDATED BY
Brennan Whitfield | Sep 27, 2024

Everyone has to remove duplicates from an array in JavaScript at some point. It’s even a common coding challenge asked during interviews.

How to Remove Duplicates From a JavaScript Array

To remove duplicate elements from a JavaScript array using the Set() method, type:

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

function removeDuplicates(arr) {
    return [...new Set(arr)];
}

console.log(removeDuplicates(arr));

The output of this sample code becomes: [1, 2, 3, 4]

With that in mind, here are some different ways to filter out duplicates from an array and return only the unique values.

 

A tutorial on different ways to remove duplicates from a JavaScript array. | Video: Code Pro

How to Remove Duplicates From JavaScript Arrays: 7 Methods to Know

There are several ways to remove duplicates from a JavaScript array and clean up your code. Below are seven methods for eliminating repeated data from your JavaScript arrays.
 

1. Filter Method

The filter method creates a new array of elements that pass the conditional we provide. And any element that fails or returns false, it won’t be in the filtered array.

Filter method code example for JavaScript array.
Filter method code example for JavaScript array. | Image: Jayanth Somineni

We can also use the filter method to retrieve the duplicate values from the array by simply adjusting our condition.

A second example for removing duplicates from JavaScript array with the filter method.
A second example for removing duplicates from JavaScript array with the filter method. | Image: Jayanth Somineni

More on JavaScriptA Guide to the Callback Function in JavaScript

 

2. Sets

Sets are a new object type with ES6 (ES2015) that allows you to create collections of unique values.

Sets method code to remove duplicates from array in JavaScript.
Sets method code to remove duplicates from array in JavaScript. | Image: Jayanth Somineni

 

3. forEach Method

By using forEach, we can iterate over the elements in the array, and we will push into the new array if it doesn’t exist in the array.

forEach method code to remove duplicates in an array.
forEach method code to remove duplicates in an array. | Image: Jayanth Somineni

 

4. Reduce Method

Reduce is always a bit trickier to understand. The reduce method is used to reduce the elements of the array and combine them into a final array based on the reducer function that you pass.

Removing duplicates from a JavaScript array using the reduce method code.
Removing duplicates from a JavaScript array using the reduce method code. | Image: Jayanth Somineni

Here is the same reduce method with a different approach:

Reduce method code to remove duplicates from JavaScript array.
Reduce method code to remove duplicates from JavaScript array. | Image: Jayanth Somineni

More on JavaScript8 Common JavaScript Data Structures

 

5. Adding a Unique Method to the Array Prototype

In JavaScript, the array prototype constructor allows you to add new properties and methods to the Array object.

Another code example for removing duplicates from an array in JavaScript.
Another code example for removing duplicates from an array in JavaScript. | Image: Jayanth Somineni

Sets will work much faster when you compare with the normal approach.

Sets method code using array prototype to remove duplicates.
Sets method code using array prototype to remove duplicates. | Image: Jayanth Somineni

 

6. Underscore JS

_.uniq method produces a duplicate-free version of the array. We can also sort this array by passing the second parameter as true.

Underscore JS method code to remove duplicates from a JavaScript array.
Underscore JS method code to remove duplicates from a JavaScript array. | Image: Jayanth Somineni

More on JavaScriptHow to Use the JavaScript Array FindIndex Method

 

7. Removing Duplicate Objects Using the Property Name

Sometimes you have to remove duplicate objects from an array of objects by the property name. We can achieve this by:

Removing duplicate objects from an array code example.
Removing duplicate objects from an array code example. | Image: Jayanth Somineni

Frequently Asked Questions

Duplicate elements can be found in a JavaScript array using various methods, including:

  1. filter()
  2. Set()
  3. indexOf() 
  4. Using an object to track elements 
  5. Using a nested for-in loop to iterate through elements and compare them

Duplicate elements in JavaScript arrays can be removed by methods like:

  1. filter()
  2. Set()
  3. indexOf()
  4. forEach()
  5. reduce()
  6. _.uniq()
Explore Job Matches.