Count Occurrences of Each Character in Java



In Java, counting the occurrences of each character in a string is a common task that can be efficiently performed using a HashMap. A HashMap allows us to store key-value pairs, where each unique character in the string is a key, and the value is the count of its occurrences.

Problem Statement

Given a string, we need to count how many times each character appears in the string. For example, in the string thisisit, the character t appears twice, h appears once, i appears three times, and s appears twice.

Input

String myStr = "thisisit";

Output

Counting occurrences of each character = {s=2, t=2, h=1, i=3}

Counting the occurrences of each character

Below are the steps to find to count the occurences of character -

    To solve this problem, we use a HashMap to store each character as a key and its occurrence count as a value. We iterate over the string and for each character:
  • Step 1. Check if the character is already a key in the HashMap using the containsKey() method.
  • Step 2. If it is, increment its count.
  • Step 3. If it is not, add it to the HashMap with a count of 1.

To count the occurrences, we are using HashMap. Loop through and using containsKey() and charAt() method, count the occurrences of each character in the above string −

HashMap <Character, Integer> hashMap = new HashMap<>();
for (int i = myStr.length() - 1; i >= 0; i--) {
   if (hashMap.containsKey(myStr.charAt(i))) {
      int count = hashMap.get(myStr.charAt(i));
      hashMap.put(myStr.charAt(i), ++count);
   } else {
         hashMap.put(myStr.charAt(i),1);
   }
}

Java program to count the occurrences of each character

Following is the program to count the occurrences of each character −

import java.util.HashMap;
public class Demo {
   public static void main(String[] args) {
      String myStr = "thisisit";
      System.out.println("String ="+myStr);
      HashMap <Character, Integer> hashMap = new HashMap<>();
      for (int i = myStr.length() - 1; i >= 0; i--) {
         if (hashMap.containsKey(myStr.charAt(i))) {
            int count = hashMap.get(myStr.charAt(i));
            hashMap.put(myStr.charAt(i), ++count);
         } else {
            hashMap.put(myStr.charAt(i),1);
         }
      }
      System.out.println("Counting occurrences of each character = "+hashMap);
   }
}

Output

String =thisisit
Counting occurrences of each character = {s=2, t=2, h=1, i=3}

Code Explanation

This Java program counts the occurrences of each character in a string using a HashMap. The string myStr is initialized with "thisisit". A HashMap is then created to store characters as keys and their counts as values. A for loop iterates through the string in reverse order. For each character, the program checks if it already exists in the HashMap using containsKey. If it does, the count is incremented; if not, the character is added with a count of 1. Finally, the program prints the HashMap, showing the count of each character in "thisisit", resulting in {s=2, t=2, h=1, i=3}.

Updated on: 2024-07-04T17:35:45+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements