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.
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. ASet
only keeps unique values.Using
filter
andindexOf
: Usefilter
withindexOf
to keep only the first occurrence of each element, effectively removing duplicates.Using
forEach
andincludes
: UseforEach
withincludes
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.
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));
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.
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));
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.
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));
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.
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.
Haven’t found what you were looking for? Contact Us
Free Resources