-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprblm229.java
More file actions
62 lines (57 loc) · 1.74 KB
/
prblm229.java
File metadata and controls
62 lines (57 loc) · 1.74 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
import java.util.*;
public class prblm229 {
public static void main(String[] args) {
int[] nums = {3,2,3};
List<Integer> ans = majorityElement2(nums);
System.out.println(ans.toString());
int[] nums2 = {1,2};
List<Integer> ans2 = majorityElement2(nums2);
System.out.println(ans2.toString());
}
public static List<Integer> majorityElement(int[] nums) {
List<Integer> list = new ArrayList<>();
Map<Integer, Integer> map = new HashMap<>();
for (int num : nums) {
map.put(num, map.getOrDefault(num, 0) + 1);
if (map.get(num) > (nums.length / 3)) {
if (list.contains(num)) {
continue;
}
else{
list.add(num);
}
}
}
return list;
}
public static List<Integer> majorityElement2(int[] nums) {
List<Integer> list = new ArrayList<>();
int count1 = 0, max1 = 0;
int count2 = 0, max2 = 0;
for (int x : nums) {
if (count1 == 0 && x != max2) {
max1 = x;
count1 = 1;
}
else if (count2 == 0 && x != max1) {
max2 = x;
count2 = 1;
}
else if(x == max1){count1++;}
else if(x == max2){count2++;}
else {
count1 -= 1;
count2 -= 1;
}
}
int c1 = 0;
int c2 = 0;
for (int x : nums) {
if(x == max1) c1++;
else if(x == max2) c2++;
}
if(c1 > (nums.length / 3)) list.add(max1);
if(c2 > (nums.length / 3)) list.add(max2);
return list;
}
}