JavaScript Program to Remove Duplicates from Array
You can remove duplicates from an array in JavaScript by using
Array.from(new Set(array)) or array.filter((item, index) => array.indexOf(item) === index).Examples
Input[1, 2, 2, 3, 4, 4, 5]
Output[1, 2, 3, 4, 5]
Input['apple', 'banana', 'apple', 'orange']
Output['apple', 'banana', 'orange']
Input[]
Output[]
How to Think About It
To remove duplicates, think about how to keep only the first occurrence of each item. You can use a special collection that stores unique values or check if the current item appears earlier in the array and skip it if it does.
Algorithm
1
Get the input array.2
Create a new collection that only keeps unique items.3
Add each item from the input array to this collection, ignoring duplicates.4
Convert the collection back to an array if needed.5
Return the array without duplicates.Code
javascript
const removeDuplicates = arr => Array.from(new Set(arr)); const numbers = [1, 2, 2, 3, 4, 4, 5]; const uniqueNumbers = removeDuplicates(numbers); console.log(uniqueNumbers);
Output
[1, 2, 3, 4, 5]
Dry Run
Let's trace the array [1, 2, 2, 3, 4, 4, 5] through the code using Set.
1
Create a Set from the array
Set contains {1, 2, 3, 4, 5} because duplicates are ignored.
2
Convert Set back to array
Array becomes [1, 2, 3, 4, 5].
3
Return the new array
Output is [1, 2, 3, 4, 5].
| Iteration | Set Content |
|---|---|
| Add 1 | {1} |
| Add 2 | {1, 2} |
| Add 2 (duplicate) | {1, 2} |
| Add 3 | {1, 2, 3} |
| Add 4 | {1, 2, 3, 4} |
| Add 4 (duplicate) | {1, 2, 3, 4} |
| Add 5 | {1, 2, 3, 4, 5} |
Why This Works
Step 1: Using Set to store unique values
A Set automatically keeps only unique items, so adding duplicates has no effect.
Step 2: Converting Set back to array
We use Array.from() to turn the Set back into a normal array.
Step 3: Returning the result
The returned array contains only the first occurrence of each item, removing duplicates.
Alternative Approaches
Using filter and indexOf
javascript
const removeDuplicates = arr => arr.filter((item, index) => arr.indexOf(item) === index); const fruits = ['apple', 'banana', 'apple', 'orange']; console.log(removeDuplicates(fruits));
This method works well but is slower for large arrays because it searches the array for each item.
Using a temporary object to track seen items
javascript
function removeDuplicates(arr) { const seen = {}; return arr.filter(item => { if (seen[item]) return false; seen[item] = true; return true; }); } console.log(removeDuplicates([1,2,2,3,4,4,5]));
This method is fast but only works well with primitive types like strings or numbers.
Complexity: O(n) time, O(n) space
Time Complexity
Using a Set, each item is added once, so time grows linearly with array size.
Space Complexity
A new Set and array are created, so extra space grows linearly with input size.
Which Approach is Fastest?
The Set method is generally fastest and simplest; filter with indexOf is slower due to repeated searches.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Set | O(n) | O(n) | Most cases, simple and fast |
| Filter + indexOf | O(n²) | O(n) | Small arrays or when Set is not available |
| Object tracking | O(n) | O(n) | Primitive types, fast but less flexible |
Use
Set for a clean and fast way to remove duplicates from arrays.Trying to remove duplicates by modifying the array while looping over it, which can cause skipped items or errors.