要求统计一个整型序列中出现次数最多的整数及其出现次数。
输入格式:
在一行中给出序列中整数个数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[] num=new int[N];
//for循环输入N个整数 3 2 -1 5 3 4 3 0 3 2
for (int i = 0; i < num.length; i++) {
num[i]=sc.nextInt();
}
//
int max=0;
int index=0;
//每一个第i个数都要和所有的数比较
for (int i = 0; i < num.length; i++) {
int count=0;
for(int j=0;j<num.length;j++) {
if(num[i]==num[j]) {
count++;
}
}
if(count>max) {
max=count;
index=i;
}
}
System.out.print(num[index]+" "+max);
}
}