Skip to content

Instantly share code, notes, and snippets.

@z7pz
Created August 3, 2021 23:00
Show Gist options
  • Save z7pz/d3d36a89d0847915bca55e27c3ba6f7f to your computer and use it in GitHub Desktop.
Save z7pz/d3d36a89d0847915bca55e27c3ba6f7f to your computer and use it in GitHub Desktop.
first non repeating char
// default: 5.187ms (nevermind)
function f(s) {
let arr = Array.apply(0, { length: 26 }).map((c) => 0)
for (let c of s.split('')) arr[c.charCodeAt(0) - 'a'.charCodeAt(0)]++;
for (let c of s.split('')) {
if (arr[c.charCodeAt(0) - 'a'.charCodeAt(0)] == 1) return c;
}
console.log(arr)
return '_'
}
console.log(
f('')
);
// default: 10.70ms (Tarek Hesham#0001)
const char = (str) => {
for (let i = 0; i < str.length; i++) {
if (str.split("").filter(x => x == str[i]).length == 1) return str[i];
};
};
console.log(char(""));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment