-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_0229.java
More file actions
29 lines (24 loc) · 833 Bytes
/
_0229.java
File metadata and controls
29 lines (24 loc) · 833 Bytes
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
package com.github.aditya;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class _0229 {
class Solution {
// 12ms beats 46.84%, 51.12MB beats 93.70%
public List<Integer> majorityElement(int[] nums) {
int third = nums.length / 3;
HashMap<Integer, Integer> freqMap = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
freqMap.put(nums[i], freqMap.getOrDefault(nums[i], 0) + 1);
}
List<Integer> result = new ArrayList<>();
for (Map.Entry<Integer, Integer> entry : freqMap.entrySet()) {
if (entry.getValue() > third) {
result.add(entry.getKey());
}
}
return result;
}
}
}