Skip to content

Instantly share code, notes, and snippets.

@bebrws
Created March 24, 2023 14:54
Show Gist options
  • Save bebrws/83ac5d790ad430983b8953ccf1100771 to your computer and use it in GitHub Desktop.
Save bebrws/83ac5d790ad430983b8953ccf1100771 to your computer and use it in GitHub Desktop.
An interview question for JS/Typescript - Find the first non repeating character in a string. Return that or null if no non repeating chars are found.
// firstNonRepeatingChar is a function that should find the first character in a string of characters that is non repeating
// non repeating meaning that it is only found in the input string (str) once
// if there are multiple non repeating characters the first non repeating character should be returned
// if all characters in the string repeat null should be returned
function firstNonRepeatingChar(str: string): string | null {
throw new Error("Please implement this function");
}
function testCase(description: string, inputString: string, expectedResult: string | null) {
const result = firstNonRepeatingChar(inputString);
let resultString;
if (result === expectedResult) {
resultString = "✅ Passed";
} else {
resultString = "❌ Failed"
}
console.log(`${resultString} ${description}: '${inputString}'`)
}
testCase("One letter in the middle is not repeating", "abdcbda", 'c');
testCase("One letter in the start is not repeating", "zbdbbda", 'z');
testCase("One letter in the start is not repeating and another in the middle", "zbdbcbda", 'z');
testCase("One letter in the start is not repeating and another in the middle", "bdb cbda", ' ');
testCase("One letter in the start is not repeating and another in the middle", "bdbb*da", '*');
testCase("All letters are repeating", "abdbdda", null);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment