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