Removing Duplicates from a JavaScript Array
Removing duplicates from an array is a very common task in Javascript, particularly when handling large datasets. So, in this article, let’s explore different methods for efficiently removing duplicate values from an array.
We have different methods to remove duplicate values, let’s go into a few methods
Methods to Remove Duplicates from an Array
1. Using Set(The Simplest Method)
The Set
object in JS only stores unique values, making its the easiest way to remove duplicates.
Example:
const numbers = [1, 2, 2, 3, 4, 4, 5]; const uniqueNumbers = [...new Set(numbers)]; console.log(uniqueNumbers);
✅Pros: Simple, fast, and concise.
❌Cons: Does not work for objects or compel data
2. Using filter()
and indexOf()
The filter()
method can be used with indexOf()
to keep only the first occurrence of each value
Example
const numbers = [1, 2, 2, 3, 4, 4, 5]; const uniqueNumbers = numbers.filter((value, index, array) => array.indexOf(value) === index); console.log(uniqueNumbers);
✅Pros: Works in all JS environments.
❌Cons: Slower for larger arrays due to repeated indexOf()
calls.
3. Using reduce()
The reduce()
method can be used to build a new array that only includes unique elements.
Example
const numbers = [1, 2, 2, 3, 4, 4, 5]; const uniqueNumbers = numbers.reduce((acc, value) => { if (!acc.includes(value)) acc.push(value); return acc; }, []); console.log(uniqueNumbers);
✅Pros: Works in all JS environments
❌Cons: Slower
Performance Comparison
For large datasets, Set() is the best choice due to its high performance
Common Use Cases
- Cleaning up user input (e.g., removing duplicate form entries).
- Ensuring unique values in an API response.
- Removing duplicate product IDs from a shopping cart.
Additional Reading:
Conclusion
Removing duplicates from a JavaScript array is essential for cleaner data. The best method depends on your needs:
✔️Use Set()
for performance
✔️Use filter()
if Set()
isn’t an option
✔️Use reduce()
for custom logic
Which method do you prefer? Let me know in the comments! 🚀