169. Majority Element
problem description
Input: [3,2,3]
Output: 3Input: [2,2,1,1,1,2,2]
Output: 2algorithm thought
code
class Solution {
public:
int majorityElement(vector<int>& nums) {
int count=0;
int res;
for(int num:nums){
if(count==0){
res=num;
count++;
continue;
}
if(num==res)
count++;
else
count--;
}
return res;
}
};algorithm analysis
Last updated