Count total number of times each character appears in the string in java

By | July 21, 2018

In previous post we learnt how to find  Count number of words in a string java
In this post we will learn how to count total number of times each character appears in the string in java

Here we are going to use map provided by Java collection framework. Logic is to read one character at a time from the string and put it in map character as key, count as value (Initial count value will be 1).

With every character that is read from the String check in the map, if it already exists as a key or not. If it exists then increment the count otherwise add the key in map with initial count value as 1.

If you run above program in your favorite IDE then you will get blow output:

You May Also Like:

Check if two strings are anagrams or not in java
How to convert string to int without using library functions in java
Check Whether a Given String/Number is a Palindrome in java
How to find first non-repeated character in a given String in Java
How to find first non-repeatable character from a String using Java 8

That’s all about How to count total number of times each character appears in the string in  java
If you have any feedback or suggestion please feel free to drop in blow comment box.

Leave a Reply

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