Majority Element

#include<map>

using namespace std;
class Solution {
public:
    int majorityElement(vector<int>& nums) {
       map<int,int> m;
       int n=nums.size();
       int i=0;
       while(i<nums.size()){
    if(m.count(nums[i]))   m[nums[i]]++;
    else m.insert(pair<int,int>(nums[i],1));
    i++;
    }
       i=0;
       map <int, int>::iterator m1_Iter;

     for ( m1_Iter = m.begin( ); m1_Iter != m.end( ); m1_Iter++ ){

      if(m1_Iter->second>n/2)
      return m1_Iter->first;

     }

 }

};

posted @ 2015-07-16 21:59  *桔子*  阅读(169)  评论(0编辑  收藏  举报