Files
30-seconds-of-code/snippets/allUnique.md
Isabelle Viktoria Maciohsek 9c7f2c1a8c Fix typos
2021-01-08 00:23:44 +02:00

19 lines
442 B
Markdown

---
title: allUnique
tags: array,beginner
---
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.
```js
const allUnique = arr => arr.length === new Set(arr).size;
```
```js
allUnique([1, 2, 3, 4]); // true
allUnique([1, 1, 2, 3]); // false
```