Remove Duplicate Elements from JavaScript Array

Remove Duplicate Elements from JavaScript Array

While working with Arrays in JavaScript, sometimes there is a need to get all unique values or remove duplicate values from them. In this blog, we will explore all the methods to remove duplicate values from an array using JavaScript.

Table of Contents:

Different Ways to Remove Duplicate Elements From JavaScript Array

JavaScript provides several methods to remove duplicate elements from an array. Here are some of them:

Method 1: Using Set() to Remove Duplicates

The Set object in JavaScript only allows unique values, it doesn’t allow duplicate values to be stored. Converting an Array to Set automatically removes duplicates.

Example:

Javascript

Output:

Using Set() to Remove Duplicates

Explanation: This example uses the Set() method to convert an Array into a Set. The Set() method works well with primitive data types like numbers or strings.

Method 2: Using filter() to Remove Duplicates

The filter() method is used to create a new array of those elements that passed a test condition. So the filter() method can be combined with indexOf() to get unique elements from an array.

Example:

Javascript

Output:

Using filter() to Remove Duplicates

Explanation:

In this example, multiple methods are used let’s understand each one by one:

  • filter() method iterates through each element over an array.
  • self.indexOf(value) returns the first position where elements appear.
  • index is the current position of a number in the array.
  • self.indexOf(value) === index is used to keep the first occurred elements only in an array and remove duplicates.

Method 3: Using reduce() to Remove Duplicates

The reduce() method can also be used to remove duplicates from an array. But, this can build a new array while checking for duplicates.

Example:

Javascript

Output:

Using remove() to Remove Duplicates

Explanation: reduce() method combined with includes() and push() used to to remove duplicates from an array in JavaScript.

Method 4: Using map() to Remove Duplicates

Map object is a special type of data structure that stores elements in key-value pairs. When we map an array into a map(). each value becomes a key in the Map. Since keys in the map are unique thus duplicate values automatically get removed.

Example:

Javascript

Output:

Using map() to Remove Duplicates

Explanation: map() method works well with objects and key-value pairs. This is one of the best ways to track unique values. Here, you use .map() to create key-value pairs like [10,10], [20,20], [20,20], etc. Now the Map stores only unique values, so duplicate values are removed. Finally, you use .values() to get unique values and convert them into the array.

Method 5: Using a for() Loop to Remove Duplicates

Using for() loop also helps you to remove the duplicate values from an array. It can check each value of the original array with each value of the output array if the match is found it doesn’t include that value in the output array.

Example:

Javascript

Output:

Using a for loop to Remove Duplicates

Explanation: In this example, outputCourses.includes(courses[i]) checks for each element of the courses array. If an element is present in the outputCourses already then there is no need to add an element again. But, if an element is not present then, the .push() method adds elements at the last index.

Performance Comparison Table

Here is the comparison between all JavaScript methods that are used to remove duplicate elements from an array:

Method Time ComplexityBest Use Case
Set()O(n)It is simple to use and best for increasing performance.
filter() + indexOf()O(n²)Used in small datasets.
reduce()O(n²)It is best for functional programming approaches.
Map()O(n)Best for object-based arrays.
for() + includes()O(n²)Simple to understand and best for small datasets.

Conclusion

Removing duplicate elements from an array in JavaScript is easy because of these built-in methods like Set(), filter(), reduce(), and map(). By choosing the right method, you can efficiently get an array with unique values.

Remove Duplicate Elements From JavaScript Array – FAQs

1. What is the purpose of indexOf() method?

The indexOf() method returns the index of the specified element in an array. If a specified value is not found in the array then it returns -1.

2. How to remove unique elements from an array?

To remove duplicates from an array in JavaScript you can use any of the methods such as filter(), Set(), reduce(), and map().

3. How to remove duplicates in the JavaScript array?

There are various methods to remove duplicates from an array in JavaScript such as the Set() method is the simple and efficient way to remove duplicates from an array. Other methods to remove duplicates are by using map(), filter(), and reduce().

4. How to remove duplicates from an unsorted array?

You can remove duplicate elements from an unsorted array by using the Set object which is one of the fastest ways to remove duplicates from an array. It can remove duplicate elements with O(n) Time Complexity.

5. How to merge two arrays in JavaScript?

You can merge two arrays in JavaScript by using a built-in method called concat() Here, is the syntax of concat() method in JavaScript.

Syntax:

arr1.concat(arr2);

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner