字符串单个字符出现次数最多算法
public class CountStringMaxSign { public String getMaxCountSign(String str){ if(str==null || str.length()==0)return null; String result = null; char chars[] = str.toCharArray(); int length = chars.length; HashMap map = new HashMap(); int max = 0; String key = null; for(int i=0;i<length;i++){ String sign = String.valueOf(chars[i]); //System.out.println(sign); if(map.containsKey(sign)){ int value = Integer.parseInt(map.get(String.valueOf(sign)).toString()); value++; map.put(sign, value); if(value>max){ max = value; key = String.valueOf(sign); } }else{ map.put(sign, 1); } } result = (key==null?"":"["+key+"][").concat(String.valueOf(max)).concat("]"); return result; } public static void main(String[] args) { CountStringMaxSign test = new CountStringMaxSign(); System.out.println(test.getMaxCountSign("abcdefgabcbbcdd")); } }