public static void findMaxCountChar(String str) { if (str == null || str == "") return; Map<String, Integer> map = new HashMap<String, Integer>(); int maxCount = 0; String maxCountStr = ""; List<String> list = new ArrayList<String>(); for (int i = 0; i < str.length(); i++) { String key = String.valueOf(str.charAt(i)); if (maxCount >= str.length() / 2) { if (maxCountStr == key) { maxCount++; } } else { if (!map.containsKey(key)) { map.put(key, 1); } else { int count = map.get(key); map.put(key, count + 1); if ((count + 1) > maxCount) { maxCount = count + 1; maxCountStr = key; list.clear(); list.add(key); } else if ((count + 1) == maxCount) { list.add(key); } } } } System.out.println("最大次数" + maxCount); if (maxCount >= str.length() / 2) { System.out.println("字符" + maxCountStr); } else { System.out.println("字符" + list); } }
原文:http://www.cnblogs.com/tonggc1668/p/7224012.html