R7-3 JAVA-求整数序列中出现次数最多的数 (10 分)
要求统计一个整型序列中出现次数最多的整数及其出现次数。
输入格式:
在一行中给出序列中整数个数N(0<N≤1000),依次给出N个整数,每个整数占一行。
输出格式:
在一行中输出出现次数最多的整数及其出现次数,数字间以空格分隔。题目保证这样的数字是唯一的。
输入样例:
在这里给出一组输入。例如:
10
3
2
-1
5
3
4
3
0
3
2
输出样例:
在这里给出相应的输出。例如:
3 4
import java.util.Scanner;
import java.math.*;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = cin.nextInt();
}
int maxn = -1 ,idx = 0;
for (int i = 0; i < n; i++) {
int cnt = 0;
for (int j = i; j < n; j++) {
if (a[i] == a[j]) {
cnt++;
}
}
if (cnt > maxn) {
maxn = cnt;
idx = a[i];
}
}
System.out.printf("%d %d",idx,maxn);
}
}