595 B
595 B
title, tags, expertise, cover, firstSeen, lastUpdated
| title | tags | expertise | cover | firstSeen | lastUpdated |
|---|---|---|---|---|---|
| Check if all array elements are unique | array | beginner | blog_images/jars-on-shelf.jpg | 2020-10-19T19:47:26+03:00 | 2021-01-08T00:23:44+02:00 |
Checks if all elements in an array are unique.
- Create a new
Setfrom the mapped values to keep only unique occurrences. - Use
Array.prototype.lengthandSet.prototype.sizeto 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