Create isDuplicates.md
This commit is contained in:
22
snippets/isDuplicates.md
Normal file
22
snippets/isDuplicates.md
Normal file
@ -0,0 +1,22 @@
|
||||
---
|
||||
title: isDuplicates
|
||||
tags: array,intermediate
|
||||
---
|
||||
|
||||
Check if there's duplicate values in a flat array.
|
||||
|
||||
- Convert original array into a Set.
|
||||
- The `.size()` returns the number of (unique) elements in a Set object.
|
||||
- Compare the lengths of the array and the Set.
|
||||
|
||||
|
||||
```js
|
||||
function isDuplicates(array) {
|
||||
return new Set(array).size !== array.length
|
||||
}
|
||||
```
|
||||
|
||||
```js
|
||||
isDuplicates([0,1,1,2]); // 'True'
|
||||
isDuplicates([0,1,2,3,]); // 'False'
|
||||
```
|
||||
Reference in New Issue
Block a user