Skip to content

Instantly share code, notes, and snippets.

@lienista
Last active August 28, 2018 07:43
Show Gist options
  • Select an option

  • Save lienista/4148e2da85a090792de801fadf88cca0 to your computer and use it in GitHub Desktop.

Select an option

Save lienista/4148e2da85a090792de801fadf88cca0 to your computer and use it in GitHub Desktop.
Algorithms in Javascript: CTCI 1.1 - Is Unique: Write a function to determine if a string has all unique characters.
const stringHasUniqueChars = (str) => {
let count = {};
for(let i=0; i<str.length; i++) {
count[str[i]] = count[str[i]] === undefined ? 1: count[str[i]]+1;
if(count[str[i]] > 1)
return false;
}
return true;
}
const s = 'Geeks for Geeks';
const y = stringHasUniqueChars(s);
const x = 'abc';
const z = stringHasUniqueChars(x);
console.log(s + ': ' + y);
console.log(x + ': ' + z);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment