• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
JavaScriptSource

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details
Home / Miscellaneous / Remove Duplicates From an Array

Remove Duplicates From an Array

Remove Duplicates From an Array

In JavaScript, Set is a collection that lets you store only unique values. This means any duplicated values are removed.

So, to remove duplicates from an array, you can convert it to a set, and then back to an array.

const numbers = [1, 1, 20, 3, 3, 3, 9, 9];
const uniqueNumbers = [...new Set(numbers)]; // -> [1, 20, 3, 9]

Here’s how it works. new Set(numbers) creates a set from a list of numbers. Creating a set automatically removes all duplicate values. The spread operator ... converts any iterable to an array. This means [...new Set(numbers)] converts the set right back to an array.

Source

https://medium.com/geekculture/20-javascript-snippets-to-code-like-a-pro-86f5fda5598e

Miscellaneous

Related Snippets:

  • Check if the given argument is a string
  • Insert an element before another one
  • Capture the right click event with JavaScript
  • Convert a string to a number

Primary Sidebar

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2025 JavaScriptSource.com

  • About
  • Privacy Policy
  • FAQ
  • Jobs For Developers