Skip to content

Latest commit

 

History

History
31 lines (23 loc) · 765 Bytes

check-if-a-flat-array-has-duplicate-values.mdx

File metadata and controls

31 lines (23 loc) · 765 Bytes
category created title updated
Validator
2021-02-25
Check if a flat array has duplicate values
2022-01-26

JavaScript version

const hasDuplicateValues = (arr) => new Set(arr).size !== arr.length;

// Or
const hasDuplicateValues = (arr) => arr.some((item, index, arr) => arr.indexOf(item) !== index);

TypeScript version

const hasDuplicateValues = <T,_>(arr: T[]): boolean => new Set(arr).size !== arr.length;

// Or
const hasDuplicateValues = <T,_>(arr: T[]): boolean => arr.some((item, index, arr) => arr.indexOf(item) !== index);

Examples

hasDuplicateValues(['h', 'e', 'l', 'l', 'o']); // true
hasDuplicateValues(['w', 'o', 'r', 'd']); // false