-
Notifications
You must be signed in to change notification settings - Fork 1
/
MigratoryBirds.java
47 lines (35 loc) · 1.24 KB
/
MigratoryBirds.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
36
37
38
39
40
41
42
43
44
45
46
47
package implementation;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
/**
* Created by tatarJR on 3/17/2017.
*/
public class MigratoryBirds {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] types = new int[n];
for (int types_i = 0; types_i < n; types_i++) {
types[types_i] = in.nextInt();
}
Map<Integer, Integer> typeCountsMap = new HashMap<Integer, Integer>();
for (int i : types) {
if (typeCountsMap.containsKey(i)) {
typeCountsMap.put(i, typeCountsMap.get(i) + 1);
} else {
typeCountsMap.put(i, 1);
}
}
Map.Entry<Integer, Integer> maxEntry = null;
for (Map.Entry<Integer, Integer> entry : typeCountsMap.entrySet()) {
if (maxEntry == null || entry.getValue() > maxEntry.getValue()) {
maxEntry = entry;
}
if (entry.getValue() == maxEntry.getValue() && entry.getKey() < maxEntry.getKey()) {
maxEntry = entry;
}
}
System.out.println("ID: " + maxEntry.getKey() + ", VALUE: " + maxEntry.getValue());
}
}