-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMajorityElement.java
More file actions
66 lines (55 loc) · 1.17 KB
/
MajorityElement.java
File metadata and controls
66 lines (55 loc) · 1.17 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package codingInterview;
import java.util.Arrays;
public class MajorityElement {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a = { 1, 2, 4, 6, 8, 9, 1, 2, 1, 3, 1, 1, 1, 5, 7, 1, 1, 1, 1, 1};
// int num = getMajority1(a);
// System.out.println(num);
int num = getMajority2(a);
System.out.println(num);
}
/**
* exist majority element
* sort the array, and return the middle element
* o(nlogn)
* @param a
* @return
*/
private static int getMajority2(int[] a) {
// TODO Auto-generated method stub
if (a.length == 1) {
return a[0];
}
Arrays.sort(a);
return a[a.length / 2];
}
/**
* supposing there is majority element sort the array, and find the majority
* element o(nlogn)
*
* @param a
* @return
*/
private static int getMajority1(int[] a) {
// TODO Auto-generated method stub
if (a.length == 1) {
return a[0];
}
Arrays.sort(a);
int count = 0;
int prev = a[0];
for (int i = 1; i < a.length; i++) {
if (prev == a[i]) {
count++;
if (count >= a.length / 2) {
return prev;
}
} else {
count = 0;
prev = a[i];
}
}
return Integer.MIN_VALUE;
}
}