Files
30-seconds-of-code/snippets/hasDuplicates.md
Isabelle Viktoria Maciohsek 42474f53f7 Fix hasDuplicates file name
2020-10-22 20:23:09 +03:00

19 lines
465 B
Markdown

---
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
```