First Non-Repeating Character in Java: Explanation & Practice

Find the first character that does not repeat

Problem summary

Write a program that finds the first character that appears only once in a string.

Starter code

public class Main {
    public static void main(String[] args) {
        String text = "programming"; // Test case 1
        
        // Find first non-repeating character
        // Print: First unique: <char> OR No unique character
    }
}

Expected output and test cases

  • p is first unique
    First unique: p
  • First unique in aabbc
    First unique: a
  • All repeat
    No unique character

Hints

  1. Count occurrence of each character first
  2. Then find first with count 1
  3. Can use array of size 26 for counts

Related Strings exercises

Practice all Strings exercises · Run this idea in the Java compiler