Remove duplicate objects from an array using javascript
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Removing duplicate objects from a JavaScript array is not the same as removing duplicate numbers or strings. Objects are compared by reference, so two separate objects with the same properties are still considered different unless you choose a key or equality rule yourself.
Why Set Alone Does Not Solve It
A Set removes duplicate references, not duplicate shapes:
That is the core reason duplicate-removal code for objects needs custom logic.
The Best Approach When Objects Have A Stable Key
If each object has a unique identifier such as id, use a Map keyed by that field:
This keeps the last object seen for each id. If you want to keep the first object instead, only set the key if it is not present:
When an id or other stable key exists, this is usually the cleanest and fastest solution.
Deduplicating By Multiple Properties
Sometimes one field is not enough, so you can build a compound key:
This works well as long as the chosen fields truly define equality for your application.
A Generic filter Pattern
If you prefer filter, use a Set of keys:
This keeps the first matching object and reads well in pipelines.
What About JSON.stringify?
You will often see this pattern:
It can work for small simple objects, but it has tradeoffs:
- Property order affects the string output.
- Functions,
undefined, and special values are not preserved reliably. - Nested objects can make the key generation expensive.
- Parsing back into objects creates new objects rather than keeping originals.
Because of that, JSON.stringify is a convenient shortcut, not a universal equality strategy.
Reusable Helper Function
A practical helper is a function that accepts a key selector:
This keeps the equality rule explicit and reusable.
Common Pitfalls
The biggest mistake is assuming object equality works like primitive equality. Two objects with identical fields are still different if they are different references.
Another pitfall is choosing the wrong deduplication key. If the key is not truly unique for the business meaning of the data, you may delete legitimate records by accident.
Developers also sometimes reach for JSON.stringify on large or complex objects without considering performance and serialization edge cases. It is fine for prototypes, but often too fragile for production logic.
Finally, be clear about whether the first duplicate should win or the last duplicate should win. Map and filter patterns can do either, but the code should make that choice obvious.
Summary
- Objects in JavaScript are compared by reference, not by property values.
- If a stable key such as
idexists, use aMaporSetkeyed by that field. - For multi-field equality, build an explicit compound key.
- '
JSON.stringifycan work for simple cases but has real limitations.' - Decide whether your deduplication should keep the first match or the last match.
Related reading
- Remove element of a regular array
- Remove elements from collection while iterating
- remove elements from link list whose sum equals to zero
- Remove empty array elements
- Remove last character of a StringBuilder?
- Remove multiple keys from Map in efficient way?
- Remove element by id
- Remove empty elements from an array in Javascript

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.