package com.hello;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Hello {
public static void main(String[] args) throws Exception {
Scanner in=new Scanner(System.in);
//String str=in.nextLine();
String str="a bb cc bkb nnn kkkkkkkkkkll t 1 ii kk";
char ch = getMaxChar(str);
System.out.println("出现最多的是:"+ch);
}
private static char getMaxChar(String str){
char[] arr = str.toCharArray();
Map<Character,Integer> map = new HashMap<Character,Integer>();
int maxValue = 0;
char maxKey = 0;
for(int i = 0; i < arr.length; i++){
Integer num = map.get(arr[i]);
if(num==null)
map.put(arr[i], 1);
else{
map.put(arr[i],++num);
if(maxValue < map.get(arr[i])){
maxValue = map.get(arr[i]);
maxKey = arr[i];
}
}
}
System.out.println(maxKey+":"+maxValue);
return maxKey;
}
}
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Hello {
public static void main(String[] args) throws Exception {
Scanner in=new Scanner(System.in);
//String str=in.nextLine();
String str="a bb cc bkb nnn kkkkkkkkkkll t 1 ii kk";
char ch = getMaxChar(str);
System.out.println("出现最多的是:"+ch);
}
private static char getMaxChar(String str){
char[] arr = str.toCharArray();
Map<Character,Integer> map = new HashMap<Character,Integer>();
int maxValue = 0;
char maxKey = 0;
for(int i = 0; i < arr.length; i++){
Integer num = map.get(arr[i]);
if(num==null)
map.put(arr[i], 1);
else{
map.put(arr[i],++num);
if(maxValue < map.get(arr[i])){
maxValue = map.get(arr[i]);
maxKey = arr[i];
}
}
}
System.out.println(maxKey+":"+maxValue);
return maxKey;
}
}