Intermediate Level
Implement the firstNonRepeatingCharacter method that returns the first character in the text that appears only once.
The input contains a string text. Your task is to return the first character that appears only once in the string.
If every character is repeated, return #. For example, in aabbcde, the first non-repeating character is c.
Example 1
Input
text (string) =
aabbcdeReturns (char)
cExample 2
Input
text (string) =
aabbReturns (char)
#Example 3
Input
text (string) =
swissReturns (char)
wFirst count how many times each character appears in the string.
Then scan the string again from left to right. The first character with frequency 1 is the answer. If no such character exists, return #.
Pseudocode:
function firstNonRepeatingCharacter(text):
frequency = empty map
for each character in text:
frequency[character]++
for each character in text:
if frequency[character] == 1:
return character
return '#'Ready to test your solutionRun Code checks your solution against the test cases.Shift + Enter to run