How can I remove duplicate values from an array in JavaScript?Davide S
To remove duplicate values from an array in JavaScript, you can use various techniques, such as using Set, filter, or reduce. Here's a step-by-step approach for each method: 1. Using Set: - The Set object in JavaScript allows you to store unique values of any type. By converting the array to a Set and then converting it back to an array, duplicate values will be automatically removed.
1 2 3 4 5
function removeDuplicates(array) { return [...new Set(array)]; }
In this example, theremoveDuplicates
function takes anarray
as a parameter. It converts the array to a Set using the spread operator (...) and thenew Set()
constructor. The Set automatically eliminates duplicate values. Finally, it converts the Set back to an array using the spread operator.
2. Using filter:
- Thefilter()
method allows you to create a new array by filtering out duplicate values.
1 2 3 4 5
function removeDuplicates(array) { return array.filter((value, index, self) => self.indexOf(value) === index); }
Here, theremoveDuplicates
function takes anarray
as a parameter. It applies thefilter()
method on the array, using an arrow function as the filter condition. The arrow function checks whether the current value's index (indexOf()) is equal to the index of the first occurrence of that value (index). If they are equal, the value is included in the filtered array.
3. Using reduce:
- Thereduce()
method allows you to iterate over the array and build a new array without duplicate values.
1 2 3 4 5 6 7 8 9 10
function removeDuplicates(array) { return array.reduce((accumulator, currentValue) => { if (!accumulator.includes(currentValue)) { accumulator.push(currentValue); } return accumulator; }, []); }
In this example, theremoveDuplicates
function takes anarray
as a parameter. It uses thereduce()
method, initializing an empty array ([]) as the initial value. The reducer function checks if theaccumulator
array already includes thecurrentValue
. If it doesn't, thecurrentValue
is added to theaccumulator
usingpush()
. The function returns theaccumulator
at each iteration.
Here's an example usage of theremoveDuplicates
function:
1 2 3 4
const array = [1, 2, 2, 3, 4, 4, 5]; const uniqueArray = removeDuplicates(array); console.log(uniqueArray); // Output: [1, 2, 3, 4, 5]
In this example, thearray
contains duplicate values. TheremoveDuplicates
function is called with thearray
as an argument. It returns a new array (uniqueArray) with duplicate values removed. The resultinguniqueArray
contains only unique values.
Choose the method that suits your needs based on factors such as simplicity, performance, or compatibility requirements.
Similar Questions
How do I remove duplicates from an array in JavaScript?
How do I remove duplicate elements from an array in JavaScript?
How do I remove an item from an array by its value in JavaScript?
How do I remove an element from an array in JavaScript?
How can I replace a specific value in an array in JavaScript?
How can I remove all occurrences of a specific element from an array in JavaScript?
How can I remove duplicates from a list in Python?
How can I check if a value is an array in JavaScript?
How can I check if a value is an array in JavaScript?
How do I remove an element from an array by index in JavaScript?
How can I remove HTML tags from a string in JavaScript?
How can I validate an email address in JavaScript?
How can I find the maximum or minimum value in an array in JavaScript?
How can I shuffle an array in JavaScript?
How can I remove all child elements from a parent element in JavaScript?
How do I calculate the sum of an array in JavaScript?
How can I split a string into an array in JavaScript?
How do I validate an array of email addresses in JavaScript?