dedupe an array

ES6, JavaScript

Remove duplicate items from an array using Set.

Explanation

  • new Set(arr): This creates a new Set object from the array, which automatically removes duplicate values.
  • [...new Set(arr)]: This converts the Set back into an array.

Usage

To deduplicate an array, use the following code:

const arr = [1, 1, 2, 2, 3, 3];
const deduped = [...new Set(arr)];
// [1, 2, 3]