s4553711
8/11/2017 - 3:41 PM

169.cpp

class Solution {
public:
    int majorityElement(vector<int>& nums) {
        int nTimes = 0;
        int candidate = 0;
        for(int i = 0; i < nums.size(); i++) {
            if (nTimes == 0) {
                candidate = nums[i];
                nTimes = 1;
            } else {
                if (candidate == nums[i])
                    nTimes++;
                else
                    nTimes--;
            }
        }
        return candidate;
        
    }
};