Skip to content

Instantly share code, notes, and snippets.

@masautt
Created September 6, 2019 18:30
Show Gist options
  • Save masautt/f1deaa07515d9cdfc1a306c8f99d1e21 to your computer and use it in GitHub Desktop.
Save masautt/f1deaa07515d9cdfc1a306c8f99d1e21 to your computer and use it in GitHub Desktop.
How to check if all elements in an array are unique in JavaScript?
function hasDuplicates(array) {
return (new Set(array)).size !== array.length;
}
console.log(hasDuplicates(["M", "A", "S", "A", "U", "T", "T"])); // --> true
console.log(hasDuplicates(["M", "A", "R", "E", "K"])); // --> false
// https://stackoverflow.com/questions/7376598/in-javascript-how-do-i-check-if-an-array-has-duplicate-values
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment