Created
March 24, 2023 14:55
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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