Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int majorityElement(vector<int>& nums) {
- int ans = 0, cnt = 0;
- for(const auto &n : nums) {
- if(cnt == 0) {
- ans = n;
- ++cnt;
- }
- else {
- (n == ans) ? ++cnt : --cnt;
- }
- }
- return ans;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement