-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMajorityElementTwo.java
More file actions
43 lines (36 loc) · 1.13 KB
/
MajorityElementTwo.java
File metadata and controls
43 lines (36 loc) · 1.13 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
package Array;
import java.util.ArrayList;
import java.util.List;
public class MajorityElementTwo {
public List<Integer> majorityElement(int[] nums) {
int count1 = 0, count2 = 0;
int candidate1 = 0, candidate2 = 0;
for (int num : nums) {
if (count1 == 0 && num != candidate2) {
count1 = 1;
candidate1 = num;
} else if (count2 == 0 && num != candidate1) {
count2 = 1;
candidate2 = num;
} else if (candidate1 == num) {
count1++;
} else if (candidate2 == num) {
count2++;
} else {
count1--;
count2--;
}
}
List<Integer> result = new ArrayList<>();
int threshold = nums.length / 3;
count1 = 0;
count2 = 0;
for (int num : nums) {
if (candidate1 == num) count1++;
else if (candidate2 == num) count2++;
}
if (count1 > threshold) result.add(candidate1);
if (count2 > threshold) result.add(candidate2);
return result;
}
}