Skip to content

Instantly share code, notes, and snippets.

@rohith2506
Created December 31, 2014 05:46
Show Gist options
  • Save rohith2506/23f8996109cbde36040b to your computer and use it in GitHub Desktop.
Save rohith2506/23f8996109cbde36040b to your computer and use it in GitHub Desktop.
class Solution {
public:
int majorityElement(vector<int> &num) {
int cnt = 0;
for(int i=0; i<num.size(); i++){
if(cnt == 0){
maj = num[i];
cnt++;
}
else if(maj == num[i])
if(cnt > n/2) return maj;
else cnt--;
}
return maj;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment