-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMajority_Element_II229.java
More file actions
116 lines (100 loc) · 3.91 KB
/
Majority_Element_II229.java
File metadata and controls
116 lines (100 loc) · 3.91 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
public class Majority_Element_II229 {
//Own solution (very weird and not efficient (woah that sounds just like me fr))
public List<Integer> majorityElement(int[] nums) {
int n = (nums.length)/3;
List<Integer> res = new ArrayList<Integer>();
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
HashSet<Integer> set = new HashSet<Integer>();
for (int num : nums) {
if (map.containsKey(num)) {
map.put(num, map.get(num) + 1);
} else {
map.put(num, 1);
}
}
for(int num: nums){
int count = map.get(num);
if(count>n){
set.add(num);
}
}
for (int num : set) {
res.add(num);
}
return res;
}
//Optimal solution using hashmap
public List<Integer> majorityElement1(int[] nums) {
// Create a frequency map to store the count of each element
Map<Integer, Integer> elementCountMap = new HashMap<>();
// Iterate through the input array to count element occurrences
for (int i = 0; i < nums.length; i++) {
elementCountMap.put(nums[i], elementCountMap.getOrDefault(nums[i], 0) + 1);
}
List<Integer> majorityElements = new ArrayList<>();
int threshold = nums.length / 3;
// Iterate through the frequency map to identify majority elements
for (Map.Entry<Integer, Integer> entry : elementCountMap.entrySet()) {
int element = entry.getKey();
int count = entry.getValue();
// Check if the element count is greater than the threshold
if (count > threshold) {
majorityElements.add(element);
}
}
return majorityElements;
}
public List<Integer> majorityElement2(int[] nums) {
int count1 = 0, count2 = 0; // Counters for the potential majority elements
int candidate1 = 0, candidate2 = 0; // Potential majority element candidates
// First pass to find potential majority elements.
for (int i = 0; i < nums.length; i++) {
// If count1 is 0 and the current number is not equal to candidate2, update candidate1.
if (count1 == 0 && nums[i] != candidate2) {
count1 = 1;
candidate1 = nums[i];
}
// If count2 is 0 and the current number is not equal to candidate1, update candidate2.
else if (count2 == 0 && nums[i] != candidate1) {
count2 = 1;
candidate2 = nums[i];
}
// Update counts for candidate1 and candidate2.
else if (candidate1 == nums[i]) {
count1++;
} else if (candidate2 == nums[i]) {
count2++;
}
// If the current number is different from both candidates, decrement their counts.
else {
count1--;
count2--;
}
}
List<Integer> result = new ArrayList<>();
int threshold = nums.length / 3; // Threshold for majority element
// Second pass to count occurrences of the potential majority elements.
count1 = 0;
count2 = 0;
for (int i = 0; i < nums.length; i++) {
if (candidate1 == nums[i]) {
count1++;
} else if (candidate2 == nums[i]) {
count2++;
}
}
// Check if the counts of potential majority elements are greater than n/3 and add them to the result.
if (count1 > threshold) {
result.add(candidate1);
}
if (count2 > threshold) {
result.add(candidate2);
}
return result;
}
}