js Intermediate
Remove duplicates in JavaScript
Open this example in our interactive code editor to experiment with it.
Try the EditorremoveDuplicates one-liner
const removeDuplicates = (array) => [...new Set(array)];
Summary
- Set is a built-in JavaScript object that only stores unique values. When you pass an array to
new Set(array), all duplicate values are automatically removed. - The spread operator
[...set]converts the Set back into a regular array, giving you a clean, duplicate-free result. - This is a concise one-liner that handles primitive values (numbers, strings, booleans) effectively. For objects or arrays as elements, you would need a custom comparison since Set checks by reference.
Try this in our interactive code editor
Practice hands-on with our built-in code sandbox.
Open Code Editor
Adrian Bigaj
Creator of BigDevSoon
Full-stack developer and educator passionate about helping developers build real-world skills through hands-on projects. Creator of BigDevSoon, a vibe coding platform with 21 projects, 100 coding challenges, 40+ practice problems, and Merlin AI.
Related Pills
js Intermediate
JavaScript multiple condition checks
Checking multiple values in JavaScript often leads to redundant, boilerplate code - let's use .includes method to fix that.
js Intermediate
Convert JavaScript data structures
Converting data structures is a day-to-day job as a Developer.
js Intermediate
ECMAScript new features
Null coalescing operator, optional chaining, and Array.flat method are all amazing.