How to remove duplicates from the array in JavaScript

Key takeaways:

  • We can remove duplicates from the array in JavaScript using the following methods:

    • Using Set: Convert an array to a Set, then back to an array to remove duplicates. A Set only keeps unique values.

    • Using filter and indexOf: Use filter with indexOf to keep only the first occurrence of each element, effectively removing duplicates.

    • Using forEach and includes: Use forEach with includes to add only unique elements to a new array.

An array is a collection of elements of the same data stored in a contiguous memory where the name of the array points to the first element. An array can contain duplicate values. Below are a few methods to remove duplicates in an array.

Remove duplicates
Remove duplicates

Method 1: Using Set

In JavaScript, we can remove duplicates from an array using a Set. A Set is a collection of unique values, so when we convert an array to a Set, it automatically removes duplicates.

function removeDuplicates(arr) {
return Array.from(new Set(arr));
}
const array = [1, 2, 3, 3, 4, 5, 5];
console.log(removeDuplicates(array));

Code explanation

  • Line 1: Declares a function named removeDuplicates that takes an array arr as input.

  • Line 2: Creates a new Set from the input array arr. Sets are collections of unique values. Array.from converts the Set back into an array.

  • Line 3: Returns the resulting array with duplicates removed.

Method 2: Using filter

We can also use the filter method along with the indexOf method to remove duplicates from an array.

function removeDuplicates(arr) {
return arr.filter((value, index) => arr.indexOf(value) === index)
}
const array = [1, 2, 3, 3, 4, 5, 5];
console.log(removeDuplicates(array));

Code explanation

  • Line 1: Declares a function named removeDuplicates that takes an array arr as input.

  • Line 2: Uses the filter method on the input array arr. For each element in the array, it checks whether the current index of the element is the same as the first index it appears in the array. If it is, the element is kept, effectively removing duplicates.

  • Line 3: Returns the resulting array with duplicates removed.

Method 3: Using forEach and includes

We can remove duplicates from an array using the forEach method, along with the includes method.

function removeDuplicates(arr) {
let uniqueArray = [];
arr.forEach(item => {
if (!uniqueArray.includes(item)) {
uniqueArray.push(item);
}
});
return uniqueArray;
}
const array = [1, 2, 3, 3, 4, 5, 5];
console.log(removeDuplicates(array));

Code explanation

  • Line 1: Declares a function named removeDuplicates that takes an array arr as input.

  • Line 2: Initializes an empty array uniqueArray to store unique elements.

  • Line 3: Iterates over each item in the input array using forEach.

  • Line 4: Checks if the current item is not already present in uniqueArray.

  • Line 5: If the item is not present, it is added to uniqueArray.

  • Line 6: Ends the if statement block.

  • Line 7: Ends the forEach loop.

  • Line 8: Returns the resulting array with duplicates removed.

  • Line 9: Ends the function.

Conclusion

These methods simplify removing duplicates, each with unique advantages. The Set method is often the fastest, while filter and forEach offer alternatives when customizing or transforming elements is needed.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


How do we check and remove duplicates in an array?

We can check and remove duplicates in an array using Array.from(new Set(array)) or array.filter((item, index) => array.indexOf(item) === index) to keep only unique values.


How can we join array and remove duplicates in JavaScript?

We can join array and remove duplicates in JavaScript by concatenate arrays with concat or spread operator, then remove duplicates with Array.from(new Set([...array1, ...array2])).


How can we sort an array in JavaScript?

We can sort an array in JavaScript using array.sort() for default ascending order or array.sort((a, b) => a - b) for numerical sorting.


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved