Skip to content

Instantly share code, notes, and snippets.

@BenDMyers
Last active May 30, 2022 09:13
Show Gist options
  • Save BenDMyers/2f75e75522b5c0c11d1e06d8c6b9506a to your computer and use it in GitHub Desktop.
Save BenDMyers/2f75e75522b5c0c11d1e06d8c6b9506a to your computer and use it in GitHub Desktop.
RWC: All Unique
/**
* Determines whether all characters in a string are unique.
*
* Original question:
* **Write a function that determines if all the characters in a given string are unique.**
* Can you do this without making any new variables? You choose if you want to include
* capitalization in your consideration for this one, as a fun challenge.
*
* @param {string} str string to test
* @param {boolean} [caseSensitive] whether to take capitalization into account
* @returns {boolean} whether all characters in the string are unique
*/
function allUnique(str, caseSensitive = true) {
return caseSensitive ?
new Set(str.split('')).size === str.length :
new Set(str.toLowerCase().split('')).size === str.length;
}
console.log(allUnique('Cassidy')); // false
console.log(allUnique('cat & dog')); // false
console.log(allUnique('cat+dog')); // true
console.log(allUnique('Cc')); // true
console.log(allUnique('Cc', false)); // false
@kelvinsekx
Copy link

Nice 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment