算法题目——求众数

在这里插入图片描述

在c++中有两个关联容器,
第一种是map,内部是按照key排序的,从小到大 
第二种是unordered_map,容器内部是无序的,使用hash组织内容的。 
#include<iostream>
#include<map>
#include<algorithm>
using namespace std;


int main()
{
    
    
	map<int,int> mp;
	int n;
	cin>>n;
	int temp;
	while(n--) 
	{
    
    
		cin>>temp;
		mp[temp]++;
	}
	int ans;
	int max=0;
	for(auto& it:mp)
	{
    
    
		if(it.second>max)
		{
    
    
			ans=it.first;
			max=it.second;
		}
		
	 }
	 cout<<ans<<endl; 
	return 0;
	
 } 

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_50675813/article/details/120582413