Back to results for “Map”
JavaScript › Arrays & Array Methods

How do you efficiently remove duplicate values from an array?

Advanced

Answer

Convert the array to a Set, which only stores unique values, then spread it back into an array. This is O(n) and far simpler than manually filtering with indexOf.
const unique = [...new Set([1,2,2,3,3,3])]; // [1,2,3]
Real-world example Deduplicating a list of tag IDs collected from multiple form submissions.

Common follow-ups: How would you dedupe an array of objects by a specific property?