Count occurrences of each character in Java String

Count occurrences of each character in Java String

In this, we present a variety of techniques for counting occurrences of each character in a Java String or the frequency of each character in a Java string. These techniques allow for the determination of how many times a specific character is repeated within a given string.

To calculate the frequency of each alphabet, we will first ask the user to input a string. We will then use an integer array of length 256 to keep track of the frequency of each character in the string. The frequency array element will be initialized with a value of zero, indicating that the count of all characters is initially zero.

Next, we will utilize a for loop to traverse the input string and increment the count of every character in the input string. Finally, we will traverse the frequency array and print the frequency of every character.

By following these steps, we can accurately determine the frequency of each character in a Java string.

Count occurrences of each character in java String using for loop

Example :

import java.util.Scanner;
import java.util.NoSuchElementException;
public class main{
 public static void main(String args[]) {
        try{
    		String str;
            int i, length, counter[] = new int[256];
     
            Scanner scanner = new Scanner(System.in);
            System.out.println("Enter a String");
            str = scanner.nextLine();
     
            length = str.length();
     
            for (i = 0; i < length; i++) {
                counter[(int) str.charAt(i)]++;
            }
     
            for (i = 0; i < 256; i++) {
                if (counter[i] != 0) {
                    System.out.println((char) i + "   --> " + counter[i]+"\n");
                }
            }
		}catch (NoSuchElementException e) {
		    System.out.println("Type something in the Stdin box above.... and then Execute");
		}	
	}	
}


Output :

Enter a String : 
developerhelps
d   --> 1

e   --> 4

h   --> 1
l   --> 2

o   --> 1

p   --> 2

r   --> 1

s   --> 1

v   --> 1

Count the occurrence of each character in string java using Hashmap

In order to determine the frequency of occurrence of each character in a given string, we utilized a HashMap. HashMap utilized the character as a key and its frequency as a value. Initially, the string is converted to a character array, and each character is evaluated sequentially. The count for each character is updated within the HashMap.

For each character, it is necessary to verify whether the key already exists in the HashMap. If the key is present, its count is incremented. Alternatively, if the key is absent, it is added to the map as a new key and assigned an initial value of 1. This approach guarantees that the frequency of every character in the given string is accurately represented within the HashMap https://www.facebook.com/developerhelps/

Example:

import java.util.HashMap;
public class EachCharacterCount  {
  
   public static void main(String[] args) 
   {
      String str = "Developerhelps";
      HashMap <Character, Integer> charCount = new HashMap<>();
      
      for (int i = str.length() - 1; i >= 0; i--) {
         if(charCount.containsKey(str.charAt(i)))
         {
            int count = charCount.get(str.charAt(i));
             charCount.put(str.charAt(i), ++count);
         }
         else 
         {
            charCount.put(str.charAt(i),1);
         }
      }
      System.out.println(charCount);
   }
}


Output :

{p=2, r=1, s=1, D=1, e=4, v=1, h=1, l=2, o=1}

Follow for More Info – https://instagram.com/developerhelps?igshid=MzRlODBiNWFlZA==

Discover Our Exciting Courses and Quiz

Enroll now to enhance your skills and knowledge!

Java Online Quiz

Level up your coding skills with our interactive programming quiz!