First character not repeated in a string

This problem asks you to find the first character in a string that is not repeated elsewhere in the string. For example, given the string "abcdab", the answer would be "c ".

Problem

Given a string, return the first character that is not repeated in the string.
Example input: "abcdab"
Example output: "c"

Solution

This solution is in Python. Our A.I. can create solutions in multiple languages.
by kayakane
def first_non_repeated(string):
    
    This function takes a string and returns the first character that is not repeated in the string.
    
    # Create a dictionary to store the number of times each character appears in the string
    char_count = {}
    # Iterate through the string
    for char in string:
        # If the character is not in the dictionary, add it and set the value to 1
        if char not in char_count:
            char_count[char] = 1
        # If the character is in the dictionary, increment the value by 1
        else:
            char_count[char] += 1
    # Iterate through the string
    for char in string:
        # If the character appears only once in the string, return it
        if char_count[char] == 1:
            return char

# Test cases
print(first_non_repeated("abcdab"))
print(first_non_repeated("abcd"))
print(first_non_repeated("aabbccdd"))
print(first_non_repeated("abc defghijklmnopqrstuvwxyz"))
print(first_non_repeated("abcdefghijklmnopqrstuvwxyza"))
print(first_non_repeated("abcdefghijklmnopqrstuvwxyzz"))
print(first_non_repeated("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"))
print(first_non_repeated("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyza"))
print(first_non_repeated("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzz"))


This solution is optimal because it iterates through the string only once to create the dictionary and then iterates through the string again to find the first character that is not repeated.

A.I. Evaluation of the Solution

The candidate's solution is correct and demonstrates a level of completeness. The candidate's approach is also optimal, as it iterates through the string only once to create the dictionary and then iterates through the string again to find the first character that is not repeated.

Evaluated at: 2022-11-17 23:36:20