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.

Categorized in:

JavaScript,

Last Update: March 22, 2024