算出字符串中出现次数最多的字符,写的比较烂 ╮(╯_╰)╭
package cn.cxy.test; import java.util.HashMap; import java.util.Map; public class CountChar { static String s = "afgafeW.7-8109-=3-012=KHLD6233"; public static void main(String[] args) { char[] c = s.toCharArray(); int[] count = new int[c.length]; int max = 0; for (int i =0; i < c.length; i++) { int counter = 1; for (int j = 0; j < c.length; j++) { if (i != j && c[i] == c[j]) { counter++; } } count[i] = counter; if (count[i] > max) { max = count[i]; } } Map<Character, Integer> map = new HashMap<Character, Integer>(); for (int i =0; i < c.length; ++i) { if (count[i] == max) { map.put(c[i], max); } } System.out.println(map); } }