JavaScript Program to Remove Duplicates From Array

Updated on December 23, 2024
Remove duplicates from array header image

Introduction

When dealing with arrays in JavaScript, a common task is to remove duplicate values to ensure that each element is unique. This is particularly useful in data processing, where redundant data can lead to inaccuracies or inefficiencies. Whether you're working with simple arrays or more complex data structures, knowing how to eliminate duplicates can greatly optimize your JavaScript programming.

In this article, you will learn how to effectively remove duplicates from an array using several methods in JavaScript. The article will guide you through different approaches, including the use of classic loops, ES6 sets, and array filter methods. By comparing these techniques, you can select the most appropriate one for your specific situation.

Using Set Objects

A Set is a built-in object that only stores unique values. When you convert an array to a set, all duplicated elements are automatically removed.

Convert Array to Set and Back to Array

  1. Initialize an array with duplicate values.

  2. Convert the array to a Set to remove duplicates.

  3. Convert the Set back to an array.

    javascript
    const arrayWithDuplicates = [1, 2, 2, 3, 4, 4, 5];
    const uniqueSet = new Set(arrayWithDuplicates);
    const uniqueArray = [...uniqueSet];
    console.log(uniqueArray);
    

    This code initializes an array with some duplicate values and then uses the Set object to retain only unique values. The spread operator ... helps convert the Set back into an array.

Using Array.filter() Method

The filter() method creates a new array with elements that pass a test provided by a function. It can be used to remove duplicates if the test checks for the first occurrence of each element.

Filter for Unique Items

  1. Define an array with duplicate entries.

  2. Use the filter() method to find and keep only the first occurrence of each element.

    javascript
    const arrayWithDuplicates = [1, 1, 2, 3, 3, 4, 5, 5];
    const filteredArray = arrayWithDuplicates.filter((item, index, array) => array.indexOf(item) === index);
    console.log(filteredArray);
    

    In this snippet, filter() checks each item's index against its first occurrence in the array (array.indexOf(item)). If the indices match, it indicates that the item is encountered for the first time, hence included in the result filteredArray.

Using forEach() and Includes()

Another approach is to iteratively build a new array by checking if an item is already included before adding it.

Build a Unique Array using forEach()

  1. Start with an original array that includes duplicates.

  2. Create an empty array to hold unique elements.

  3. Iterate over the original array, adding elements to the new array only if they are not already included.

    javascript
    const arrayWithDuplicates = [2, 3, 3, 1, 5, 2, 5, 7];
    let uniqueArray = [];
    arrayWithDuplicates.forEach(item => {
        if (!uniqueArray.includes(item)) {
            uniqueArray.push(item);
        }
    });
    console.log(uniqueArray);
    

    The forEach loop goes through each element, and the includes() method checks if an element is already in uniqueArray. If it isn't, the element is added, ensuring all elements in uniqueArray are unique.

Conclusion

Removing duplicates from an array in JavaScript can be accomplished in several effective ways, each suitable for different scenarios. Use Set for a simpler and shorter approach, filter() for a method that integrates well with more complex conditions, and forEach combined with includes() for situations where you might want to execute additional logic while processing each unique item. Master these techniques to efficiently handle arrays and maintain data integrity by ensuring elements are unique, enhancing both performance and reliability of your applications.