Ninja technique🥷 to ACE DSA Interviews.
class Solution {
public:
// moore's voting algorithm
int majorityElement(vector<int>& nums)
{
int count = 0;
int candidate = 0;
for (int x : nums) {
if (count == 0) {
candidate = x;
}
// count += (x == candidate) ? 1 : -1;
if (x == candidate) count++;
else count--;
}
return candidate;
}
};