public class test {
public static void main(String[] args) {
System.out.println(findMaxValue("abcda"));
}
public static Character findMaxValue(String str){
if (str == null || str == ""){
return null;
}
Character maxChar = null;
int maxCount = 0;
Map<Character,Integer> map = new HashMap<Character, Integer>();//将出现的字母和出现的次数放进一个map中
for(int i=0; i < str.length();i++){
if (map.containsKey(str.charAt(i)))
map.put(str.charAt(i),map.get(str.charAt(i))+1);//如果map的key包含该字符,则+1
else
map.put(str.charAt(i),1);//如不map的key不包含该字符,则初始化为1
if (maxCount < map.get(str.charAt(i))){
maxCount = map.get(str.charAt(i));
maxChar = str.charAt(i);
}
}
System.out.println("字母"+ maxChar+"出现次数为"+maxCount);
return maxChar;
}
}
统计一个字符串字符出现最多的次数
最新推荐文章于 2023-07-12 23:21:25 发布