-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest.java
More file actions
33 lines (32 loc) · 1002 Bytes
/
Test.java
File metadata and controls
33 lines (32 loc) · 1002 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
30
31
32
33
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Test {
public static void main(String[] args) {
int[] arr = {1};
Test tool = new Test();
System.out.println(tool.MoreThanHalfNum_Solution(arr));
}
public int MoreThanHalfNum_Solution (int[] numbers) {
// write code here
Map<Integer,Integer> map = new HashMap<>();
for(int i : numbers){
if (map.containsKey(i)){
map.put(i,map.get(i)+1);
}else {
map.put(i,1);
}
}
int max = Integer.MIN_VALUE;
int result = 0;
Set<Map.Entry<Integer, Integer>> entries = map.entrySet();
for(Map.Entry<Integer,Integer> entry : entries){
if (entry.getValue()>max){
max=entry.getValue();
result = entry.getKey();
}
}
return result;
}
}