Remove duplicate items from an array using Set
.
Explanation
new Set(arr)
: This creates a newSet
object from the array, which automatically removes duplicate values.[...new Set(arr)]
: This converts theSet
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]