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) = aabbcde
Returns (char) c

Example 2

Input
text (string) = aabb
Returns (char) #

Example 3

Input
text (string) = swiss
Returns (char) w

First 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

Notifications

Updates are enabled for this browser.

Receive new tutorials, articles and learning resources. You can turn updates off anytime.

Test browser code

Use this code in admin to send a test notification to this browser.