-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path169.cpp
More file actions
32 lines (27 loc) · 679 Bytes
/
169.cpp
File metadata and controls
32 lines (27 loc) · 679 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// 169. Majority Element - https://leetcode.com/problems/majority-element
#include "bits/stdc++.h"
using namespace std;
// https://gregable.com/2013/10/majority-vote-algorithm-find-majority.html
class Solution {
public:
int majorityElement(vector<int>& nums) {
int n = (int)nums.size();
int result = 0;
int count = 0;
for (int num : nums) {
if (count == 0) {
result = num;
}
if (result == num) {
++count;
} else {
--count;
}
}
return result;
}
};
int main() {
ios::sync_with_stdio(false);
return 0;
}