Java program to check occurence of each character in String


In order to find occurrence of each character in a string we can use Map utility of Java.In Map, a key could not be duplicate so make each character of the string as the key of Map and provide an initial value corresponding to each key as 1 if this character does not insert in the map before. Now when a character repeats during insertion as key in Map increase its value by one. Continue this for each character until all characters of string get inserted.

Example

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

Output

{D=1, T=4, S=2, R=4, Y=3}

Updated on: 25-Jun-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements