Skip to content

Instantly share code, notes, and snippets.

@bebrws
Created March 24, 2023 14:55
Show Gist options
  • Save bebrws/59e3bcc81653903b047b08e8d86b486c to your computer and use it in GitHub Desktop.
Save bebrws/59e3bcc81653903b047b08e8d86b486c to your computer and use it in GitHub Desktop.
An interview question in Javascript - 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) {
throw new Error("Please implement this function");
}
function testCase(description, inputString, expectedResult) {
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