Home » Java Program to Count the Occurrences of Each Character

Java Program to Count the Occurrences of Each Character

by Online Tutorials Library

Java Program to Count the Occurrences of Each Character

In this section, we will discuss how to count the frequency of characters in a string. For example, consider the word, Tutoraspire. In the given string, the frequency of the letter j is 1 a is 2, v is 1, t- is 2, p is 1, o is 1, i is 1, and n is 1. The same, we will perform through a Java program with different approaches.

There are many solutions to count the occurrence of each character some of them are:

  • Using Naive Approach
  • Using Counter Array
  • Using Java HashMap
  • Using Java 8

Using Naive Approach

It is the simplest approach to count the occurrence of each character.

CountOccuranceOfChar1.java

Output:

The occurrence of P is: 1  The occurrence of n is: 4  The occurrence of e is: 1  The occurrence of u is: 2  The occurrence of m is: 2  The occurrence of o is: 9  The occurrence of l is: 3  The occurrence of t is: 1  The occurrence of r is: 2  The occurrence of a is: 2  The occurrence of i is: 6  The occurrence of c is: 6  The occurrence of s is: 4  The occurrence of p is: 1  The occurrence of v is: 1  

Using Counter Array

In the following Java program, we have used the counter array to count the occurrence of each character in a string. We have defined a for loop that iterates over the given string and increments the count variable by 1 at index based on character. Again, we have iterated over the counter array and print character and frequency if counter[i] is not 0.

CountOccuranceOfChar1.java

Output:

Please enter a string: TutorAspire a --> 2  i --> 1  j --> 1  n --> 1  o --> 1  p --> 1  t --> 2  v --> 1  

Using Java HashMap

In the following Java program, we have used Java HashMap to count the occurrence of each character in the given string. We know that the HashMap stores key and value pairs and does not hold the duplicate key. The program stores character as a key and the occurrence of character as a value.

First, we have converted the given string into the character array and iterate over the character array one by one. Update the count value in the HashMap. After that, for each character, we need to verify if the key already exists in the HashMap or not. If exists, increase the count variable, else add it to the map as a new key and provide the initial value with count 1.

CountOccuranceOfChar2.java

Output:

{e=1, g=2, l=1, o=2}  

Using Java 8

In the following Java program, we have used the Java 8 features. First, we have initialized a string of which occurrence of the character is to be counted. After that, we have created an instance of the Java Map<>. We have performed various intermediate operations to get the occurrence of the character.

First, the given string is split into the array by using the String.split() method. After that, the Arrays.stream() method returns a stream of the passed array.

The second intermediate operation is to convert the string into lowercase letters. For the same, we have used the Stream.map() method that returns a stream consisting of the results of applying the given function to the elements of this stream.

The collect() function is used to perform a mutable reduction operation and concatenate the list elements. The Collectors.groupingBy() method returns a Collector implementing a cascaded “group by” operation on input elements of type T.

In order to count the elements, we have used the counting() method of the Collectors class. The method returns a Collector accepting elements of type T. It counts the number of input elements, if no elements are present, the result is 0.

CountOccuranceOfChar3.java

Output:

{c=2, o=2, m=2, u=1, n=2, i=2, a=1, t=1}  

Let’s see another logic for the same.

The logic of the following Java program is the same as the above program except for some things. In the following Java program, we have used the Java Pattern class.

First, we have invoked the compile() method that compiles the given regular expression into a pattern. After that, we have called the matcher() function that creates a matcher (match the given input against this pattern).

At last, we have created an object of the Map, count the elements that are stored in the map. After counting, iterate over the map by using the forEach() loop. The solution works for the Java 8 or letter versions.

CountOccuranceOfChar4.java

Output:

m = 1 times  o = 3 times  n = 1 times  p = 1 times  l = 1 times  y = 1 times  

You may also like