-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMajorityElement169.java
31 lines (30 loc) · 1.02 KB
/
MajorityElement169.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
import java.util.HashMap;
import java.util.Map;
public class MajorityElement169 {
private static Map<Integer, Integer> countNums(int[] nums){
Map<Integer, Integer> counts = new HashMap<Integer, Integer>();
for (int num : nums) {
if (!counts.containsKey(num)) {
counts.put(num, 1);
}else{
counts.put(num, counts.get(num)+1);
}
}
return counts;
}
public static int majorityElement1(int[] nums) {
Map<Integer, Integer> counts = countNums(nums);
Map.Entry<Integer,Integer> majorityEntry = null;
for (Map.Entry<Integer,Integer> entry : counts.entrySet()) {
if (majorityEntry == null || entry.getValue() > majorityEntry.getValue()) {
majorityEntry = entry;
}
}
return majorityEntry.getKey();
}
public static void main(String[] args) {
int[] nums = {2,2,1,1,1,2,2};
int n = majorityElement1(nums);
System.out.println(n);
}
}