map查找重复次数最多的数字

#include<bits/stdc++.h>
using namespace std;
int main()
{
    map<int,int> Map;
    int n;
    cin>>n;
    int a;
    for(int i=0;i<n;i++)
    {
        cin>>a;
        if(Map.count(a))
        {
            Map[a]++;
        }
        else
        {
            Map[a]=1;
        }
    }
    int Max=-1;
    int flag;
    map<int ,int>::iterator it;
    for( it=Map.begin();it!=Map.end();it++)
    {
        if(it->second > Max)
        {
            flag=it->first;
            Max=it->second;
        }
    }
    cout<<"重复次数最多的数字是:"<<flag<<" 重复了"<<Max<<"次"<<endl;
}

猜你喜欢

转载自blog.csdn.net/QLU_minoz/article/details/81626970