#279 Finding the First Non-Repeating Character in a String

Companies:
Itone
Themes:
Collections & GenericsStringsPractice

Find the first non-repeating character in a string:

public class First {
    //"cba" => c
    //"abc" => a
    //"abca" => b
    public static void main(String[] args) {
        checkResult("cba", 'c');
        checkResult("abc", 'a');
        checkResult("abca", 'b');
        checkResult("abcab", 'c');
        checkResult("abcabcd", 'd');
    }

    public static Character getFirstUnique(String st) {
        ...
    }


    private static void checkResult(String input, Character expectedResult) {
        Character actualCharacter = getFirstUnique(input);
        if (Objects.equals(expectedResult, actualCharacter)) {
            System.out.println(actualCharacter + " OK");
        } else {
            System.out.println("Check failed! Input '" + input + "'; Result'" + actualCharacter + "'; Expected '" + expectedResult + "'!");
        }
    }
}

Question review

Mentor
Answer as you would in a real interview. I’ll ask follow-up questions, then compare your answer with the reference answer and show your accuracy as a %.
Sign in to start the dialogue The mentor keeps the context of the current question and your previous messages.
Sign in