#279 Finding the First Non-Repeating Character in a String
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
Sign in to start the dialogue
The mentor keeps the context of the current question and your previous messages.
Sign in
Available with Premium
This is just one of the solutions available with Premium. And more:
- 250+ advanced lectures
- 1300+ practice exercises
- 500+ solutions to problems from real interviews
- Personalized Java Hub certificates
- Expanded access to the AI mentor