寻找数组中重复次数最多的数

#include<iostream>
#include<map>
using namespace std;

int helper(const int a[],const int n)
{
  map<int,int> m;
  for(int i = 0;i<n;i++)
   m[a[i]]++;
  map<int,int>::iterator comp = m.begin();
  for( map<int,int>::iterator it = comp;it != m.end();it++)
      if(comp->second < it->second)
      comp = it;
  return comp->second;
}

int main()
{
    int a[] = {1,2,3,4,4,4,5,5,5,5,6};
    cout<<helper(a,sizeof(a)/sizeof(a[0]));
}

 

posted on 2014-05-18 18:40  berkeleysong  阅读(237)  评论(0编辑  收藏  举报

导航