Files
30-seconds-of-code/snippets/allUnique.md
Isabelle Viktoria Maciohsek a727c1eaea Update allUnique.md
2020-10-19 22:17:39 +03:00

441 B

title, tags
title tags
allUnique array,beginner

Checks if all elements in an array are unique.

  • Create a new Set from the mapped values to keep only unique occurences.
  • 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