-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP120812.java
More file actions
29 lines (24 loc) · 718 Bytes
/
Copy pathP120812.java
File metadata and controls
29 lines (24 loc) · 718 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 intro;
import java.util.*;
public class P120812 {
public static void main(String[] args) {
// int[] arr = {1, 2, 3, 3, 3, 4};
int[] arr = {1, 2,1, 2, 2};
System.out.println("result = " + solution(arr));
}
static int solution(int[] array){
Map<Integer, Integer> map = new HashMap<>();
int answer = 0;
int maxFreq = 0;
for (int num : array) {
map.put(num, map.getOrDefault(num, 0) + 1);
if(maxFreq < map.get(num)){
maxFreq = map.get(num);
answer = num;
}else if(maxFreq == map.get(num)){
answer = -1;
}
}
return answer;
}
}