Skip to content

Instantly share code, notes, and snippets.

@transparenceweb
Last active May 31, 2022 17:18
Show Gist options
  • Select an option

  • Save transparenceweb/befaf91dc24e995bb90ca6b79cb703aa to your computer and use it in GitHub Desktop.

Select an option

Save transparenceweb/befaf91dc24e995bb90ca6b79cb703aa to your computer and use it in GitHub Desktop.
Cassidoo code question #250
function allUnique(string) {
if (string.length !== (new Set(string)).size) {
console.log(`Duplicate characters, same case, in '${string}'`);
return false;
} else {
if (string.length !== (new Set(string.toLowerCase()).size)) {
console.log(`Duplicate characters across cases in '${string}'`);
return false;
} else {
console.log(`No duplication in '${string}'`);
return true;
}
}
}
console.log(allUnique("Blue"));
console.log(allUnique("Yellow"));
console.log(allUnique("Purple"));
@transparenceweb

Copy link
Copy Markdown
Author

Problem

  • 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.

Approach

  • Avoided variable creation by testing passed parameter against manipulated versions of itself
  • First, test that no characters are duplicated in the same case
  • Then, test if there are no direct duplicates, can I rule out instances where capitalisation would be the only difference
  • If I can rule out both of those, return true for allUnique

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