-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMajorityElement.java
29 lines (27 loc) · 996 Bytes
/
MajorityElement.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
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
public class MajorityElement {
public static void main(String[] args) {
MajorityElement element = new MajorityElement();
int[] nums = {3,2,3};
System.out.print(element.majorityElement(nums));
}
public int majorityElement(int[] nums) {
Map<Integer, Integer> counts = new HashMap<>();
for (int i=0; i<nums.length; i++) {
Integer count = counts.get(nums[i]);
count = count != null ? count + 1 : 1;
counts.put(nums[i], count);
}
int majorityElementCount = Integer.MIN_VALUE;
int majorityElement = Integer.MIN_VALUE;
for (Entry<Integer, Integer> entry:counts.entrySet()) {
if (entry.getValue() > majorityElementCount) {
majorityElementCount = entry.getValue();
majorityElement = entry.getKey();
}
}
return majorityElement;
}
}