Files
30-seconds-of-code/snippets/js/s/array-has-duplicates.md
Angelos Chalaris 9d032ce05e Rename js snippets
2023-05-19 20:23:47 +03:00

570 B

title, type, language, tags, cover, dateModified
title type language tags cover dateModified
Check if array has duplicates snippet javascript
array
beach-pineapple 2020-10-22T20:23:09+03:00

Checks if there are duplicate values in a flat array.

  • Use Set to get the unique values in the array.
  • Use 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