-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathmajority.java
45 lines (35 loc) · 969 Bytes
/
majority.java
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
//Given an array of size n, find the majority element. The majority element is the element that appears more than floor(n/2) times.
//
//You may assume that the array is non-empty and the majority element always exist in the array.
//
//Example :
//
//Input : [2, 1, 2]
//Return : 2 which occurs 2 times which is greater than 3/2.
//
public class Solution {
public int majorityElement(final List<Integer> A) {
if (A == null)
return -1;
int maj = A.get(0);
int count = 1;
int n = A.size();
for (int i = 1; i < n; i++) {
if (A.get(i) == maj) {
count++;
} else if (count == 1) {
maj = A.get(i);
} else {
count--;
}
}
count = 0;
for (int i = 0; i < n; i++) {
if (A.get(i) == maj)
count++;
}
if (count > n / 2)
return maj;
return -1;
}
}