Merge pull request #1275 from jun-low/new/is-duplicate

Add isDuplicates
This commit is contained in:
Angelos Chalaris
2020-10-04 16:16:39 +03:00
committed by GitHub

18
snippets/isDuplicates.md Normal file
View File

@ -0,0 +1,18 @@
---
title: hasDuplicates
tags: array,beginner
---
Checks if there are duplicate values in a flat array.
- Use `Set()` to get the unique values in the array.
- Use `Set.prototype.size` and `Array.prototype.length` to check if the count of the unique values is the same as elements in the original array.
```js
const hasDuplicates = arr => new Set(arr).size !== arr.length;
```
```js
hasDuplicates([0,1,1,2]); // true
hasDuplicates([0,1,2,3,]); // false
```