How to get distinct characters and their count in a String in java

How to get distinct characters and their count in a String in java

For Explanation watch the video:

code :

import java.util.*;
import java.lang.*;
import java.io.*;
class Codechef{
    public static void main(String[] args){
        String s = "abcdABCDabcd";
        Map<Character,Integer> hm = new HashMap<>();
        for(int i=0;i<s.length();i++){
            Character ch = s.charAt(i);
            hm.put(ch,hm.getOrDefault(ch,0)+1);
        }
        System.out.println(hm);
    }
}

output :

img.png