bluehost-banner

How to Remove Array Duplicates in ES6

In this tutorial, we will learn about How to Remove Array Duplicates in ES6


1.Using Set


Set is a new data object introduced in ES6. Because Set only lets you store unique values. When you pass in an array, it will remove any duplicate values.


const array = [4, 1, 2, 4, 5, 3];


const uniqueSet = new Set(array);
// Set {4, 1, 2, 5, 3}


const newArray = [...uniqueSet];
// [4, 1, 2, 5, 3]


In the above example, we created a new set by passing an array, and Set only allows unique values, so all duplicates will be removed.

Then we need to convert it back to an array using the spread operator(...)