-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMajorityElementII.java
35 lines (33 loc) · 986 Bytes
/
MajorityElementII.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
package io.ziheng.hashtable.leetcode;
import java.util.List;
import java.util.LinkedList;
import java.util.Map;
import java.util.HashMap;
/**
* LeetCode 229. Majority Element II
* https://leetcode.com/problems/majority-element-ii/
*/
public class MajorityElementII {
public List<Integer> majorityElement(int[] nums) {
if (nums == null || nums.length == 0) {
return new LinkedList<>();
}
List<Integer> resultList = new LinkedList<>();
Map<Integer, Integer> map = new HashMap<>();
int n = nums.length;
for (int num : nums) {
if (!map.containsKey(num)) {
map.put(num, 1);
} else {
map.put(num, 1 + map.get(num));
}
}
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue() > n / 3) {
resultList.add(entry.getKey());
}
}
return resultList;
}
}
/* EOF */