js Intermediate

Remove duplicates in JavaScript

Remove duplicates in JavaScript
Remove Duplicates Demo

Open this example in our interactive code editor to experiment with it.

Try the Editor

removeDuplicates 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
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.