First non-repeating character in a string.

Given a string, find the first non-repeating character. For example, in the string "abacabad", the first non-repeating character is 'c'.

Problem

Find the first non-repeating character in a string.
Input: "abacabad"
Output: 'c'

Solution

This solution is in C++. Our A.I. can create solutions in multiple languages.
by kayakane
#include <iostream>
#include <string>
#include <unordered_map>

using namespace std;

char firstNonRepeatingChar(string str) {
    unordered_map<char, int> charCount;
    for (char c : str) {
        charCount[c]++;
    }
    for (char c : str) {
        if (charCount[c] == 1) {
            return c;
        }
    }
    return '\0';
}

int main() {
    string str = "abacabad";
    cout << firstNonRepeatingChar(str) << endl;
    return 0;
}

A.I. Evaluation of the Solution

The candidate's solution correctly finds the first non-repeating character in a string. The solution uses a hash map to keep track of the number of times each character appears in the string, then loops through the string again to find the first character that only appears once. This is a solid solution that correctly solves the problem. The candidate demonstrates a good understanding of how to use hash maps and how to loop through a string.

Evaluated at: 2022-12-08 04:15:23