How to Remove Duplicates From an Array in JavaScript
Remove duplicate values from a JavaScript array using Set, filter(), or reduce(), including how to dedupe arrays of objects by a key.
Published September 16, 2026
The shortest way to remove duplicates in JavaScript is to pass the array through a Set, which only stores unique values. This article shows that, plus how to dedupe arrays of objects by a key.
The concept
A Set is a collection that never holds the same value twice. Converting an array to a Set and back drops every repeat while keeping the first occurrence order.
Primitive values (numbers, strings) compare by value, but objects compare by reference, so two objects with identical contents are still considered different. For objects you must choose a key to dedupe on.
Watch duplicates disappear
Practical examples
Using Set
const unique = [...new Set([1, 2, 2, 3])] // [1, 2, 3]Using filter + indexOf
Keeps an item only if it is the first time its value appears. Slower (O(n²)) but needs no ES6 features.
const unique = arr.filter((v, i) => arr.indexOf(v) === i)Objects by key
A Map keyed by id keeps one object per id (the last one wins).
const users = [{id:1,n:'A'},{id:2,n:'B'},{id:1,n:'A'}]
const unique = [...new Map(users.map(u => [u.id, u])).values()]Common mistakes
- Expecting new Set([{a:1},{a:1}]) to dedupe objects — it won't, they are different references.
- Forgetting to spread the Set back into an array; a Set is not an array.
- Using indexOf on huge arrays, which is quadratic.
Key takeaways
- [...new Set(arr)] is the go-to for primitives.
- For objects, dedupe by a key using a Map.
- filter + indexOf works but is slower.
FAQ
Does Set keep the original order?
Yes, a Set iterates in insertion order, so the first occurrence of each value is kept in place.
How do I dedupe case-insensitively?
Map each value to lowercase for the key, for example new Map(arr.map(s => [s.toLowerCase(), s])).