Alphabetical Character Frequency in Java: Explanation & Practice

Count frequency of each character

Problem summary

Print each unique character and its frequency in alphabetical order.

Starter code

public class Main {
    public static void main(String[] args) {
        String str = "hello"; // Test case 1
        
        // Print frequency of each character
        // Format: char:count (one per line, sorted)
    }
}

Expected output and test cases

  • hello frequencies
    e:1
    h:1
    l:2
    o:1
  • abab frequencies
    a:2
    b:2
  • aaa frequencies
    a:3

Hints

  1. Use int array of size 26 for lowercase
  2. Index = char - 'a'
  3. Sort alphabetically when printing

Related Strings exercises

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