Update allUnique.md

This commit is contained in:
Isabelle Viktoria Maciohsek
2020-10-19 22:17:39 +03:00
committed by GitHub
parent ba33e9e1de
commit a727c1eaea

View File

@ -3,17 +3,16 @@ title: allUnique
tags: array,beginner tags: array,beginner
--- ---
Checks if all elements in an array are equal and returns`true` else returns `false`. Checks if all elements in an array are unique.
- Use `Array.prototype.length` property to return the number of elements in that array. - Create a new `Set` from the mapped values to keep only unique occurences.
- Use `Set()` object to collect unique elements from the given array and then the `Set.prototype.size` to get the number of elements in the set - Use `Array.prototype.length` and `Set.prototype.size` to compare the length of the unique values to the original array.
- Check the two values for equality
```js ```js
const allUnique = myArray => myArray.length === new Set(myArray).size; const allUnique = arr => arr.length === new Set(arr).size;
``` ```
```js ```js
allUnique([1,2,3,4,5]); // true allUnique([1, 2, 3, 4]); // true
allUnique([1,1,2,3,4]); // false allUnique([1, 1, 2, 3]); // false
``` ```