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 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 aSet, then back to an array to remove duplicates. ASetonly keeps unique values.Using
filterandindexOf: UsefilterwithindexOfto keep only the first occurrence of each element, effectively removing duplicates.Using
forEachandincludes: UseforEachwithincludesto 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.
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
removeDuplicatesthat takes an arrayarras input.Line 2: Creates a new
Setfrom the input arrayarr. Sets are collections of unique values.Array.fromconverts 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
removeDuplicatesthat takes an arrayarras input.Line 2: Uses the
filtermethod on the input arrayarr. 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
removeDuplicatesthat takes an arrayarras input.Line 2: Initializes an empty array
uniqueArrayto 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
ifstatement block.Line 7: Ends the
forEachloop.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?
How can we join array and remove duplicates in JavaScript?
How can we sort an array in JavaScript?
Free Resources