Update isDuplicates.md
This commit is contained in:
@ -1,22 +1,18 @@
|
|||||||
---
|
---
|
||||||
title: isDuplicates
|
title: hasDuplicates
|
||||||
tags: array,intermediate
|
tags: array,beginner
|
||||||
---
|
---
|
||||||
|
|
||||||
Check if there's duplicate values in a flat array.
|
Checks if there are duplicate values in a flat array.
|
||||||
|
|
||||||
- Convert original array into a Set.
|
|
||||||
- The `Set.prototype.size` returns the number of (unique) elements in a Set object.
|
|
||||||
- Compare the lengths of the array and the Set.
|
|
||||||
|
|
||||||
|
- 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
|
```js
|
||||||
function isDuplicates(array) {
|
const hasDuplicates = arr => new Set(arr).size !== arr.length;
|
||||||
return new Set(array).size !== array.length
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```js
|
||||||
isDuplicates([0,1,1,2]); // 'True'
|
hasDuplicates([0,1,1,2]); // true
|
||||||
isDuplicates([0,1,2,3,]); // 'False'
|
hasDuplicates([0,1,2,3,]); // false
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user