Merge pull request #1639 from komputarist/master

Added allUnique snippet
Resolves #1638
This commit is contained in:
Isabelle Viktoria Maciohsek
2020-10-19 22:18:48 +03:00
committed by GitHub

18
snippets/allUnique.md Normal file
View File

@ -0,0 +1,18 @@
---
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 occurences.
- 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
```