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/fdc36b327c4829ff592c7092873471c2 to your computer and use it in GitHub Desktop.

Select an option

Save lienista/fdc36b327c4829ff592c7092873471c2 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 checker = 0; //integer used to represent 32-bit operator
for (let i = 0; i < str.length; i++) {
let bitAtIndex = str[i] - 'a';
if ((checker & (1 << bitAtIndex)) > 0) {
return false;
}
checker = checker | (1<<bitAtIndex);
}
return true;
}
let s = 'Geeks for Geeks';
let y = stringHasUniqueChars(s);
let x = 'abc';
let 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