算出字符串中出現次數最多的字符,寫的比較爛 ╮(╯_╰)╭

灬猜想灬 15年前發布 | 5K 次閱讀 表達式 Jafka Kafka SSD

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);
}

} </pre>

 本文由用戶 灬猜想灬 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!