Skip to content

Instantly share code, notes, and snippets.

@elersong
Created February 11, 2021 21:03
Show Gist options
  • Save elersong/ea98a3bd76b080542fa5ef3516c5a181 to your computer and use it in GitHub Desktop.
Save elersong/ea98a3bd76b080542fa5ef3516c5a181 to your computer and use it in GitHub Desktop.
Check for duplicate array values
// test if string contains any duplicate chars
function isUniq(str) {
let letters = {};
str.split('').forEach(char => {
if (letters[char]) {
letters[char] = false;
} else {
letters[char] = true;
}
});
// If the letters object has any false values, there's one or more duplicates
return !Object.values(letters).includes(false);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment