Leetcode 169 Solution
This article provides solution to leetcode question 169 (majority-element)
Access this page by simply typing in "lcs 169" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
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;
}
};