forked from lennylxx/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path169.c
More file actions
31 lines (23 loc) · 644 Bytes
/
169.c
File metadata and controls
31 lines (23 loc) · 644 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
#include <stdio.h>
int majorityElement(int num[], int n) {
int major;
int i;
major = num[0];
int count = 1;
for (i = 1; i < n; i++) {
if (num[i] == major) count ++;
else count --;
if (count == 0) {
major = num[i];
count = 1;
}
}
return major;
}
int main() {
int num1[] = { 1, 2, 2, 3, 3, 3, 3, 4};
int num2[] = { 4, 5, 4};
printf("%d\n", majorityElement(num1, sizeof(num1)/sizeof(num1[0])));
printf("%d\n", majorityElement(num2, sizeof(num2)/sizeof(num2[0])));
return 0;
}