给你一个整型数组如{1,3,4,7,2,1,1,5,2}, * 打印出现次数最多的那个数,如果最多的次数相同,则打印数字大的那个数。

/*
* 给你一个整型数组如{1,3,4,7,2,1,1,5,2},
* 打印出现次数最多的那个数,如果最多的次数相同,则打印数字大的那个数。
*/
public class Test6 {

public static void main(String[] args) {
    Test();
}

public static void Test() {
    int count1, count2 = 0, max = 0;
    int[] a = { 9, 3, 7, 7, 9, 9, 7, 10, 7, 5, 9, 7, 5, 8 };
    for (int i = 0; i < a.length; i++) {
        count1 = 0;
        for (int j = i; j < a.length; j++) {

            if (a[i] == a[j]) {
                count1++;
            }
        }

        if (count2 < count1) {
            count2 = count1;
            max = a[i];

        } 

        else if (count1 == count2) {
            if (max < a[i]) {
                max = a[i];

            }
        }
    }
    System.out.println("出现次数最多的数字是:" + max + ",出现的次数是:" + count2);
}

}

原文地址:https://www.cnblogs.com/MountDa/p/13174809.html