Files
30-seconds-of-code/snippets/isDuplicates.md
2020-10-04 20:45:05 +08:00

438 B

title, tags
title tags
isDuplicates array,intermediate

Check if there's duplicate values in a flat array.

  • Convert original array into a Set.
  • The .size() returns the number of (unique) elements in a Set object.
  • Compare the lengths of the array and the Set.
function isDuplicates(array) {
  return new Set(array).size !== array.length
}
isDuplicates([0,1,1,2]); // 'True'
isDuplicates([0,1,2,3,]); // 'False'