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

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Using JavaScript to Remove Duplicates From Array

Using JavaScript to Remove Duplicates From Array

September 15, 2022 Leave a Comment

We can use JavaScript to remove duplicates from an array by converting the array to a set with the new Set() method and then back to an array with the Array.from() method.

var numbersArray = [0,7,7,7,0,2,3,1,1,4,5,6,7];

var convertToSet = new Set(numbersArray);

var convertBackToArray = Array.from(convertToSet);

console.log(convertBackToArray);

#Output:
[0, 7, 2, 3, 1, 4, 5, 6]

Look below to see a more compact version of this code.


When working with collections of data in JavaScript, a very common task to do is remove duplicates.

To remove duplicates from an array in JavaScript, the easiest way is by converting the array to a set with the new Set() method and then back to an array with the Array.from() method.

A set is an unordered collection of unique elements. On the other hand, arrays can contain duplicates.

Converting an array to a set creates a set with the same items as the array and removes all duplicate values.

Below is our example again of how to remove duplicates from an array with the new Set() method in JavaScript.

var numbersArray = [0,7,7,7,0,2,3,1,1,4,5,6,7];

var convertToSet = new Set(numbersArray);

var convertBackToArray = Array.from(convertToSet);

console.log(convertBackToArray);

#Output:
[0, 7, 2, 3, 1, 4, 5, 6]

We can put our code in a function, removeDuplicates() to make it easy to reuse this code.

function removeDuplicates(arr){
  return Array.from(new Set(arr));
};

And finally, let’s see this function in action using our same example.

function removeDuplicates(arr){
  return Array.from(new Set(arr));
};

var numbersArray = [0,7,7,7,0,2,3,1,1,4,5,6,7];

console.log(removeDuplicates(numbersArray));

#Output:
[0, 7, 2, 3, 1, 4, 5, 6]

Hopefully this article has been useful for you to learn how to use JavaScript to remove duplicates from an array.

Other Articles You'll Also Like:

  • 1.  Using JavaScript to get Textarea Value
  • 2.  How to Change an Image on Hover Using JavaScript
  • 3.  Using JavaScript to Check if Array is Empty
  • 4.  JavaScript Change Text Color of a Paragraph
  • 5.  outerHTML – How to Get and Change the outerHTML Property of an Element
  • 6.  Using JavaScript to Get the Length of Array
  • 7.  JavaScript acos – Find Arccosine and Inverse Cosine of Number
  • 8.  Using JavaScript to Generate a Random Float Between 0 and 1
  • 9.  Using JavaScript to Replace Multiple Characters in a String
  • 10.  Using JavaScript to Remove the Last Character From a String

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy