Files
30-seconds-of-code/snippets/hasDuplicates.md
Isabelle Viktoria Maciohsek 8bf67754ce Make expertise a field
2022-03-01 20:21:45 +02:00

566 B

title, tags, expertise, firstSeen, lastUpdated
title tags expertise firstSeen lastUpdated
Check if array has duplicates array beginner 2020-10-22T20:23:09+03:00 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