Skip to content

shinnn/array-has-duplicates

Repository files navigation

array-has-duplicates

npm version Build Status Coverage Status

Check if an Array includes duplicated values or not

arrayHasDuplicates([1, 2, 3, 4, 5]); //=> false
arrayHasDuplicates([1, 2, 3, 4, 5, 1]); //=> true

Installation

Use npm.

npm install array-has-duplicates

API

arrayHasDuplicates(array)

array: Array<any>
Return: boolean

It returns true if the array includes at least one pair of duplicated values, otherwise returns false.

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 level.

arrayHasDuplicates([0, -0]); //=> false

Note that it ignores all empty indexes.

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 constructor serves the purpose.

function arrayHasDuplicatesLoose(arr) {
  return arr.length !== new Set(arr).size;
}

License

ISC License © 2018 Shinnosuke Watanabe

About

Check if an array includes duplicated values or not

Resources

License

Stars

Watchers

Forks

Packages

No packages published