字符串查找重复字符最多的
public class FindChar{ public static void main(String[] args){ findChar("什么是什么意义咦咦咦咦"); } public static void findChar(String msg){ if(null == msg) return; Map<String,String> charMap = new HashMap<String,String>(); int base = 0; String maxChar = ""; for(int i=0; i < msg.length(); i++){ String word = String.valueOf(msg.charAt(i)); if(!charMap.containsKey(word)){ charMap.put(word, "1"); }else{ int index = Integer.valueOf(charMap.get(word)) + 1; charMap.put(word, String.valueOf(index)); if(index > base){ base = index; maxChar = word; } } } // 出现最多次数 System.out.println(base); // 出现做多次数字符 System.out.println(maxChar); } }
忘记在网上哪里看到过的。不是原创--特此声明