Beginner JavaScript Interview QuestionsBeginnerPractical
JavaScript · Question 32
How can you remove duplicate values from an array in JavaScript?
Direct answer
For primitive values using SameValueZero-style equality, the common concise solution is [...new Set(array)].
A Set stores unique values, so const unique = [...new Set(items)]; is a clean solution for many arrays of primitives.
For objects, two separately created objects are different identities even if their properties look identical. If duplicates should be determined by a field such as id, use explicit key-based logic, often with a Map or Set of seen keys.