-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Solution.java
41 lines (34 loc) · 1003 Bytes
/
Solution.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
// github.com/RodneyShag
import java.util.Scanner;
// Time Complexity: O(n)
public class Solution {
static final int NUM_TYPES = 5;
static int migratoryBirds(int[] birds) {
/* Get counts of each type */
int[] count = new int[NUM_TYPES + 1];
for (int num : birds) {
count[num]++;
}
/* Find max */
int maxIndex = 1;
for (int i = 0; i < count.length; i++) {
if (count[i] > count[maxIndex]) {
maxIndex = i;
}
}
return maxIndex;
}
public static void main(String[] args) {
/* Save input */
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[] birds = new int[n];
for (int i = 0; i < n; i++){
birds[i] = scan.nextInt();
}
scan.close();
/* Calculate result */
int result = migratoryBirds(birds);
System.out.println(result);
}
}