获取字符串中出现最多次数的字符

package edu.cumt.jnotnull;

public class Test {

	public static void main(String[] arg) {
		String str = "acdadfaadfasdfasdfasdfadfasdfafawefaefaavavavvzvzSS";
		int[] strCounts = new int[255];//基本 ASCII 是 128 个,扩展 ASCII 是 256 个。 
		int biggestCount = 0;
		char biggestCh = 0;
		char ch = 0;
		int currentCount = 0;
		for (int i = str.length() - 1; i >= 0; i--) {
			ch = str.charAt(i);
			currentCount = ++strCounts[ch];
			if (currentCount > biggestCount) {
				biggestCount = currentCount;
				biggestCh = ch;
			}
		}
		System.out.println(biggestCh);
		System.out.println(biggestCount);
	}

}

 

你可能感兴趣的:(字符串)