DEV Community

Discussion on: Remove Duplicates The Shortest Way

Collapse
 
briancodes profile image
Brian • Edited

Just to note that Set values are compared by reference for uniqueness - this can catch you out if your Array contains objects rather than primitive types

Collapse
 
islam profile image
Islam Sayed

Great advice! But how can we consider objects as duplicates even if they have same properties when they are passed by reference. So I think objects case is not applicable to this problem.

Collapse
 
briancodes profile image
Brian • Edited

If you considered these objects duplicates [{id:1}, {id:1}], the Set approach won't suit. In this case though you'd probably need to use a utility library like Lodash: _.uniqWith(objects, _.isEqual)