Remove Duplicate Values From JavaScript Array: Fast & Easy Methods

Feat 15567 Png

Imagine spending hours building a JavaScript application, only to find that the same data keeps showing up again and again. Duplicate values in a JavaScript array can slow things down, make results confusing, and even cause users to lose trust in what they see. When arrays are used everywhere from sending JSON data to powering APIs, handling them well is not just helpful, it is essential.

No one wants to scroll through the same joke twice or see repeated information in an app. That is why learning how to remove duplicate values from a JavaScript array is so important. With the right methods, it is possible to clean up any array in seconds and keep everything running smoothly. This guide will show fast and easy ways to get rid of duplicates, so every JavaScript array stays neat and reliable.

Key Takeaways

  • Removing duplicate values from a JavaScript array helps keep our data clean and our applications running smoothly.
  • We can use the Set method to quickly create a new array with only unique values.
  • The filter method, combined with indexOf, lets us keep only the first occurrence of each value in the array.
  • Using a forEach loop, we can build a new array by adding values only if they are not already included.
  • The reduce method is another way to build an array of unique values by checking each item as we go.
  • For arrays of objects, we can remove duplicates by comparing a specific property of each object.
  • Choosing the right method depends on our needs, but Set is often the fastest and easiest for most cases.

Methods to Remove Duplicate Values From Array

Below are several ways to remove duplicates from a JavaScript array.

1. indexOf() Method

In JS, indexof() will give the index number of a value if it is present in an array and if not, then it returns -1. We can use this property to create an array with unique values only. 

We can write code to create a new array and then use a for loop or forEach to iterate over all the elements in the original array containing duplicated values and only if the indexof() of a value is -1 then push it to the new array otherwise move on to the next iteration.

Example 1: For loop with indexOf() Method

let arr = ['John', 'Paul', 'George', 'John', 'Ringo', 'George'];

let uniqueArr = [];

for (let i = 0; i < arr.length; i++) {
    if (uniqueArr.indexOf(arr[i]) === -1) {
        uniqueArr.push(arr[i]);
    }
}

console.log(uniqueArr);

Output:

[ 'John', 'Paul', 'George', 'Ringo' ]

Example 2: forEach with indexOf() Method

let arr = ['John', 'Paul', 'George', 'John', 'Ringo', 'George'];

let uniqueArr = [];

arr.forEach(function (element) {
    if (uniqueArr.indexOf(element) === -1) {
        uniqueArr.push(element);
    }
});

console.log(uniqueArr);

Output:

[ 'John', 'Paul', 'George', 'Ringo' ]

If you want to learn more about a for loop, click here, and for forEach(), we have a dedicated guide, Node.js forEach() method, which is exactly the same in JavaScript.

2. includes() Method

This method checks whether a particular value exists in an array or not. If it exists it returns true, if not then it returns false.

We can create a new empty array and then loop through the old array, if the value does not exist in the new array then push it, if it exists then move on to the next iteration. This will gradually make us get a new array with non-duplicate values.

Example 1: For loop with includes() Method

let arr = ['John', 'Paul', 'George', 'John', 'Ringo', 'George'];

let uniqueArr = [];

for (let i = 0; i < arr.length; i++) {
    if (uniqueArr.includes(arr[i]) === false) {
        uniqueArr.push(arr[i]);
    }
}

console.log(uniqueArr);

Output:

[ 'John', 'Paul', 'George', 'Ringo' ]

Example 2: forEach with includes() Method

let arr = ['John', 'Paul', 'George', 'John', 'Ringo', 'George'];

let uniqueArr = [];

arr.forEach(function (element) {
    if (uniqueArr.includes(element) === false) {
        uniqueArr.push(element);
    }
});

console.log(uniqueArr);

Output:

[ 'John', 'Paul', 'George', 'Ringo' ]

3. Set Class in Javascript

We can transfer an array into a set using the Set class. Transforming into a set will automatically remove all duplicated elements.

The array is passed as an argument to the instance of the Set class, which returns a set containing only unique elements. Then we can transform the set into an array to get the array with no duplicated values.

Example:

let arr = ['John', 'Paul', 'George', 'John', 'Ringo', 'George'];

const setArr = new Set(arr);

let uniqueArr = [...setArr];

console.log(uniqueArr);  

Output:

[ 'John', 'Paul', 'George', 'Ringo' ]

4. filter() Method

This method is used to filter the array on some condition, as an argument to this method we can pass a callback that returns the values if its index value is the same as indexof().

Example:

let arr = ['John', 'Paul', 'George', 'John', 'Ringo', 'George'];

let uniqueArr = arr.filter((element, index) => {
    return arr.indexOf(element) === index;
});

console.log(uniqueArr);

Output:

[ 'John', 'Paul', 'George', 'Ringo' ]

Common Pitfalls When Removing Duplicates in JavaScript

Forgetting That Objects and Arrays Are Compared by Reference

When we try to remove duplicates from an array of objects or arrays, we might expect JavaScript to treat objects with the same properties as duplicates. But JavaScript compares objects and arrays by reference, not by value. For example, [1, 2] and [1, 2] are seen as different, even though they look the same. The same goes for {a: 1} and {a: 1}. If we use a method like Set or indexOf on arrays of objects, it will not remove these duplicates. To solve this, we can use JSON.stringify to convert objects to strings before checking for duplicates. For example, when we want to remove duplicate jokes from an array of joke objects, we can use filter and findIndex with JSON.stringify to compare the whole object, not just the reference.

Not Handling Case Sensitivity in Strings

When we remove duplicates from an array of strings, we might forget that JavaScript treats “Apple” and “apple” as different values. This can lead to unwanted duplicates if we want to treat them as the same. For example, if we have [“Apple”, “apple”, “APPLE”], using Set will keep all three. To fix this, we can convert all strings to the same case, like lower case, before removing duplicates. This way, we get only one version of the word in our final array.

Assuming Set Removes All Types of Duplicates

The Set method is fast and easy for removing duplicates from arrays of numbers or strings, but it does not work as expected for arrays of objects or arrays inside arrays. For example, Set will not remove [1, 2] if it appears twice, because each is a different reference in memory. This can cause confusion if we expect Set to handle every case. The solution is to use a method that works for the type of data in the array, such as filter with findIndex for objects, or JSON.stringify for more complex data.

Frequently Asked Questions

What is a JavaScript array and why do we use it?

A JavaScript array is a way to group similar or different types of data together, which helps us organize, store, and work with lists of values in our programs.

Why is it important to remove duplicate values from a JavaScript array?

Removing duplicate values helps our code run faster and ensures that we only work with unique data, which makes our applications more reliable and efficient.

What problems can duplicates in an array cause?

Duplicates can slow down our code, lead to incorrect results, and create a bad experience for users, especially if we are displaying repeated information like the same joke more than once.

What is the fastest way to remove duplicates from a JavaScript array?

The fastest way is to use the Set object, which only keeps unique values, and then turn it back into an array.

How do we use the Set method to remove duplicates?

We create a Set from our array, which removes duplicates, and then use the spread operator to convert the Set back into a new array with only unique values.

Can we remove duplicates without using Set?

Yes, we can use the filter method with indexOf, or use a for loop to check each value and add it to a new array only if it is not already there.

How does the filter and indexOf method work for removing duplicates?

The filter method checks each value and keeps it only if its first appearance in the array matches its current position, so only the first occurrence of each value is kept.

Is there a way to remove duplicates using a for loop?

Yes, we can loop through the array, check if each value is already in a new array, and add it only if it is not, which gives us an array with no duplicates.

Can we remove duplicate objects from an array?

Yes, but we need to check a specific property of each object, like an ID or name, to decide if it is a duplicate, since objects are compared differently from simple values.

Are there any built-in JavaScript methods or libraries for this task?

Yes, we can use built-in methods like Set, filter, and reduce, or use libraries like Underscore JS which has a uniq method for removing duplicates.

Do these methods work in all browsers?

Most modern methods like Set work in newer browsers, but for older browsers, we can use filter and indexOf which are widely supported.

AI Dev Assistant Tips

Artificial Intelligence tools like GitHub Copilot and ChatGPT can make working with JavaScript arrays much easier. When we want to remove duplicate values from an array, these tools can help us write code faster and find the best method for our needs. We can ask for code examples, explanations, or even get help debugging our solutions.

Copy-Paste-Ready AI Prompt

Show me different ways to remove duplicate values from a JavaScript array. Include examples using Set, filter, and for loop. Explain which method is fastest and why.

  • We can ask AI tools for step-by-step code examples, so we can compare different methods easily.
  • It is helpful to ask for explanations along with the code, so we understand why a method works and when to use it.
  • If we get stuck, we can describe our problem to the AI and ask for help fixing errors or improving our code.
  • We should try different prompts to get the most helpful answer. For example, we can ask for performance tips or how to handle arrays with objects.
  • It is a good idea to review the code and test it in our own project, because AI suggestions might need small changes to fit our needs.

Summary

An Array can contain multiple types of values together and sometimes these values present more than once which creates redundancy in the array. To remove this duplicated value we have many methods in JavaScript such as converting an array into a set or using the include() method, the indexof() method, or the filter() methods with different combinations of loops. We hope this tutorial helps you to remove duplicate values from an array in JavaScript.

References

About the Author

Aditya Gupta is a Full Stack Developer with over 3 years of experience building scalable and maintainable web applications. He is passionate about writing clean code and helping other developers level up their skills in the modern JavaScript ecosystem.

Connect on LinkedIn: https://www.linkedin.com/in/dev-aditya-gupta/

My Mission: To demystify complex programming concepts and empower developers to build better software.

Review Your Cart
0
Add Coupon Code
Subtotal