Rename js snippets
This commit is contained in:
22
snippets/js/s/array-element-uniqueness.md
Normal file
22
snippets/js/s/array-element-uniqueness.md
Normal file
@ -0,0 +1,22 @@
|
||||
---
|
||||
title: Check if all array elements are unique
|
||||
type: snippet
|
||||
language: javascript
|
||||
tags: [array]
|
||||
cover: strawberries
|
||||
dateModified: 2021-01-08T00:23:44+02:00
|
||||
---
|
||||
|
||||
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
|
||||
```
|
||||
Reference in New Issue
Block a user