Files
30-seconds-of-code/snippets/js/s/all-unique.md
2023-05-07 16:07:29 +03:00

560 B

title, type, language, tags, cover, dateModified
title type language tags cover dateModified
Check if all array elements are unique snippet javascript
array
jars-on-shelf 2021-01-08T00:23:44+02:00

Checks if all elements in an array are unique.

  • Create a new Set from the mapped values to keep only unique occurrences.
  • Use Array.prototype.length and Set.prototype.size to compare the length of the unique values to the original array.
const allUnique = arr => arr.length === new Set(arr).size;
allUnique([1, 2, 3, 4]); // true
allUnique([1, 1, 2, 3]); // false