Leetcode 169 Solution

This article provides solution to leetcode question 169 (majority-element)

https://leetcode.com/problems/majority-element

Solution

class Solution {
public:
    int majorityElement(vector<int>& nums) {
        int s = 0;
        int lastval = 0;

        for (int i = 0; i < nums.size(); i++)
        {
            if (s == 0)
            {
                lastval = nums[i];
                s++;
            }
            else if (lastval == nums[i])
                s++;
            else
            {
                s--;
            }
        }

        return lastval;
    }
};