Created
December 31, 2014 05:46
-
-
Save rohith2506/23f8996109cbde36040b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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