- Notifications
You must be signed in to change notification settings - Fork 117
/
Copy path169.c
31 lines (23 loc) · 644 Bytes
/
169.c
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>
intmajorityElement(intnum[], intn) {
intmajor;
inti;
major=num[0];
intcount=1;
for (i=1; i<n; i++) {
if (num[i] ==major) count++;
elsecount--;
if (count==0) {
major=num[i];
count=1;
}
}
returnmajor;
}
intmain() {
intnum1[] = { 1, 2, 2, 3, 3, 3, 3, 4};
intnum2[] = { 4, 5, 4};
printf("%d\n", majorityElement(num1, sizeof(num1)/sizeof(num1[0])));
printf("%d\n", majorityElement(num2, sizeof(num2)/sizeof(num2[0])));
return0;
}