Count the Number of Occurrences of a Character in a String in Java

Count the Number of Occurrences of a Character in a String in Java

In this article, we will look at a problem: Given an Input String and a Character, we have to Count the Occurrences of such Character in the given String.

For Example, If the Given String is : "Java2Blog" and we have to count the occurrences of Character ‘a’ in the String.

💻 Awesome Tech Resources:
  • Looking for ⚒️ tech jobs? Go to our job portal.
  • Looking for tech events? Go to tech events 🗓️ Calendar.️

Therefore, Count of 'a' is : 2 , in the String “Java2Blog” . We will discuss different approaches to do this :

Note: The search of the Character will be Case-Sensitive for any String.

1. Using String Library Methods

This is the most conventional and easiest method to follow in order to find occurrences of character in a string . For this, we use the charAt() method present in the String Class of java.lang package. The signature of method is :

public char charAt(index) – index of the character to be found.

  • Iterate throughout the length of the input String
  • Check whether each character matches the character to search
    • If yes, increment the count
    • else do nothing
Let us look at the implementation:


2. Using Recursion

This is a rather interesting and tricky solution. Here, we call our function for the start index 0 of the String. If character matches to searched character, we add 1 to our result variable and recur for index+1, until we reach the end point of the string.

Let us look at the code:


3. Using Hashing Concept

Using Arrays

In this approach, we use a primitive integer type array of size 26. Then for each character in the string we encounter we increment it’s hash value in the array. For each character, char its index in array will be : Arr[ char – 97].

Note: This is a Case Sensitive Approach. If we use Uppercase Characters the index value will be: Arr[ char – 65]. The space we use is constant does not depend on size of input String.

Let us look at the implementation.

Using Collections (Map)

Here we will do the same thing as above for each character in the input string we will put it in our Map with value 1, if it is seen for the first time. If the character repeats or the character is already present in our Map, we update the value of the character in the map by adding 1 to it. We can use any type of Map; HashMap, TreeMap, In this example we used a simple HashMap.

Note: This approach works for both Uppercase and Lowercase Characters. Hence, Case In-Sensitive.


4. Using Java 8 Features

If we use Java 8 or above JDK Version, we can use the feature of lambdas and Stream to implement this. We use the IntStream class methods, chars method and codePoints method which returns the integer codepoints (Unicode value) of the character stream. We then filter them using lambda expression with the search character and count their occurrences using count method. We will look at each of their implementation.


So that’s it for the article, you can try out with other examples and execute the code for better understanding.


You may also like:

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.