465 B
465 B
title, tags
| title | tags |
|---|---|
| hasDuplicates | array,beginner |
Checks if there are duplicate values in a flat array.
- Use
Set()to get the unique values in the array. - Use
Set.prototype.sizeandArray.prototype.lengthto check if the count of the unique values is the same as elements in the original array.
const hasDuplicates = arr => new Set(arr).size !== arr.length;
hasDuplicates([0, 1, 1, 2]); // true
hasDuplicates([0, 1, 2, 3]); // false