输入格式
输入的第一行只有一个正整数n(1 ≤ n ≤ 1000),表示数字的个数。
输入的第二行有n个整数s1, s2, …, sn (1 ≤ si ≤ 10000, 1 ≤ i ≤ n)。相邻的数用空格分隔。
输出格式
输出这n个次数中出现次数最多的数。如果这样的数有多个,输出其中最小的一个。
样例输入
6
10 1 10 20 30 20
样例输出
10
#include<iostream>
using namespace std;
int n;
int arr[9999];
void input() //输入
{
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
}
void output()
{
int a[9999];//a[i]代表i出现的次数
for (int i = 0; i < 9999; i++)
{
a[i] = 0;//a[]初始化为0
}
for (int i = 0; i < n; i++)//输入数组中数字每出现一次,对应位置的a[]数组就加一
{
int y = arr[i];
a[y]++;
}
int d = 0;
for (int i = 0; i < 9999;i++)//求最多出现次数是多少
{
if (a[i] > d)
{
d = a[i];
}
}
for (int i = 0; i < 9999; i++)//找出a[]中第一次出现最多次数的下标,即所求数
{
if (a[i] == d)
{
cout << i<<endl;
break;
}
}
}
int main()
{
input();
output();
return 0;
}