Remove Duplicates in a JavaScript Array

A simple and clean method to remove all duplicates in a given array.

Remove duplicates using a Set

We will be using a set, introduced in ES6. It’s a data structure with data that cannot be repeated. If we would have an array like this:

const list = [1, 2, 2, 3];

We could easily remove the duplicates by combining the set syntax with the array syntax, combined with the spread operator.

const listWithoutDuplicates = [...newSet(list)]; // [1, 2, 3]

This basically creates a unique set with the given array, and uses the spread operator ... as well as the square brackets [] to create a new array from that set. Easy and clean.