Set()
to get the unique values in the array.Set.prototype.size
and Array.prototype.length
to 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
Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️