[Home] >  Validator >  Check if a flat array has duplicate values

Check if a flat array has duplicate values

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