leetcode中Majority Element问题

##问题描述

Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.

You may assume that the array is non-empty and the majority element always exist in the array.

即寻找出现次数最多的数值

##C++代码
class Solution {
public:
int majorityElement(vector &num) {
int vote = num[0];
int count = 1;
int size = num.size();
//vote from the second number
for( int i = 1; i < size; i++ )
{
if( count == 0 ) { vote = num[i]; count++; }
else if( vote == num[i] ) count++;
else count–;
}
return vote;
}
};

##算法分析