Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/shinnn/array-has-duplicates
Check if an array includes duplicated values or not
https://github.com/shinnn/array-has-duplicates
Last synced: 26 days ago
JSON representation
Check if an array includes duplicated values or not
- Host: GitHub
- URL: https://github.com/shinnn/array-has-duplicates
- Owner: shinnn
- License: isc
- Created: 2016-03-03T09:37:32.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-11-29T00:54:14.000Z (almost 6 years ago)
- Last Synced: 2024-10-11T21:16:16.674Z (about 1 month ago)
- Language: JavaScript
- Homepage: https://npm.runkit.com/array-has-duplicates
- Size: 72.3 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# array-has-duplicates
[![npm version](https://img.shields.io/npm/v/array-has-duplicates.svg)](https://www.npmjs.com/package/array-has-duplicates)
[![Build Status](https://travis-ci.com/shinnn/array-has-duplicates.svg?branch=master)](https://travis-ci.com/shinnn/array-has-duplicates)
[![Coverage Status](https://img.shields.io/coveralls/shinnn/array-has-duplicates.svg)](https://coveralls.io/github/shinnn/array-has-duplicates)Check if an `Array` includes duplicated values or not
```javascript
arrayHasDuplicates([1, 2, 3, 4, 5]); //=> false
arrayHasDuplicates([1, 2, 3, 4, 5, 1]); //=> true
```## Installation
[Use](https://docs.npmjs.com/cli/install) [npm](https://docs.npmjs.com/about-npm/).
```
npm install array-has-duplicates
```## API
### arrayHasDuplicates(*array*)
*array*: `Array`
Return: `boolean`It returns `true` if the array includes at least one pair of duplicated values, otherwise returns `false`.
```javascript
arrayHasDuplicates([3, 3, 3]); //=> true
arrayHasDuplicates([3, '3', [3]]); //=> false
```*"Duplicated"* means a value is identical to another value in the ECMAScript [same-value equality](https://developer.mozilla.org/docs/Web/JavaScript/Equality_comparisons_and_sameness#Same-value_equality) level.
```javascript
arrayHasDuplicates([0, -0]); //=> false
```Note that it ignores all empty indexes.
```javascript
arrayHasDuplicates([, undefined]);
//=> false, though array[0] and array[1] are both `undefined`
```## Tips
When the difference between `-0` and `+0` is not important, there is no need to use this module and the [`Set`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set) constructor serves the purpose.
```javascript
function arrayHasDuplicatesLoose(arr) {
return arr.length !== new Set(arr).size;
}
```## License
[ISC License](./LICENSE) © 2018 Shinnosuke Watanabe