First non-repeating character in a string

Given a string, this function returns the first non-repeating character in the string. For example, given the input string "abcab", the function would return "c".

Problem

Given a string, return the first non-repeating character in the string.
Example input: "abcab"
Example output: "c"

Solution

This solution is in Python. Our A.I. can create solutions in multiple languages.
by kayakane
def first_non_repeating_char(string):
    
    This function takes a string as input and returns the first non-repeating character in the string.
    The function uses a dictionary to store the number of times each character appears in the string.
    The function then iterates through the string and returns the first character that appears only once.
    The function is optimal because it only iterates through the string once and uses a dictionary to store the number of times each character appears in the string.
    
    char_count = {}
    for char in string:
        if char in char_count:
            char_count[char] += 1
        else:
            char_count[char] = 1
    for char in string:
        if char_count[char] == 1:
            return char

print(first_non_repeating_char("abcab"))

A.I. Evaluation of the Solution

The candidate's solution is correct and demonstrates a level of completeness. The solution uses a dictionary to store the number of times each character appears in the string and then iterates through the string to find the first character that appears only once. The solution is optimal because it only iterates through the string once.

Evaluated at: 2022-11-18 06:15:51