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