程序以在Java中找到给定字符串中字符的频率
#java #字符串 #frequency #interviewquestion

代码:

import java.util.*;
import java.lang.*;
class Demo{
    public static void main(String[] args){
        String name = "idiot";
        Map<Character,Integer> map = new HashMap<Character,Integer>();
        for(int i=0;i<name.length();i++){
            Character ch = name.charAt(i);
            map.put(ch,map.getOrDefault(ch,0)+1);
        }
        System.out.println(map);
        for(Map.Entry<Character,Integer> e : map.entrySet()){
            System.out.println(e.getKey()+" "+e.getValue());
        }
    }   

}

o/p:

Image description