unique for arrays in JavaScript
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
JavaScript does not have a built-in Array.prototype.unique() method, but creating a unique array is still easy and common. The best approach depends on what the array contains, because primitives and objects need different strategies.
The Modern Default: Set
For arrays of primitive values such as numbers, strings, and booleans, Set is the cleanest solution.
This works because Set stores only unique values and preserves insertion order.
You can also write it with Array.from:
The result is the same.
A Reusable unique Helper
If you want a function you can call throughout your codebase:
This is the most idiomatic helper for primitive arrays in modern JavaScript.
Older Alternative: filter Plus indexOf
Before Set became the obvious answer, a common pattern was:
This still works, but it is more verbose and usually slower on larger arrays because indexOf performs repeated searches.
Objects Need a Different Strategy
Set removes duplicates by reference for objects, not by deep content. That means this array still contains two separate objects:
If your objects have a natural unique key, use a Map keyed by that property:
That keeps the last object for each id. If you want the first one instead, reverse the choice or build the map conditionally.
Preserving the First Match
Here is a helper that keeps the first object for a given key:
This is often the most practical "unique array" utility in application code.
Equality Details Matter
For primitives, Set handles values the way modern JavaScript collections define sameness. That gives intuitive results for normal use:
- duplicate strings collapse
- duplicate numbers collapse
- duplicate booleans collapse
For objects and arrays, equality is by reference. Two separate objects with the same fields are still different values unless you create your own keying rule.
Sorting Is Separate from Deduplication
Uniqueness and sorting are different operations. If you need both, do them deliberately:
Do not assume deduplication automatically sorts the data.
Common Pitfalls
The biggest pitfall is using Set on objects and expecting deep equality. Set only knows whether two object references are the same object.
Another pitfall is reaching for the older filter and indexOf pattern in modern code when Set is clearer and usually more efficient.
A third pitfall is forgetting that deduplication preserves insertion order rather than applying any custom ordering logic.
Finally, if the array can contain mixed types, be explicit about what "duplicate" should mean before choosing a helper.
Summary
- For primitive arrays,
[...]new Set(array)is the cleanest unique pattern - '
Array.from(new Set(array))is an equivalent alternative' - Objects need a key-based strategy such as
Mapor a customuniqueByhelper - Deduplication does not imply sorting
- The right
uniquesolution depends on how equality should be defined for the values in the array

