-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBirdsMigratingTheMost.java
More file actions
37 lines (29 loc) · 910 Bytes
/
BirdsMigratingTheMost.java
File metadata and controls
37 lines (29 loc) · 910 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
34
35
36
37
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Arrays;
public class BirdsMigratingTheMost {
public int getId(List<Integer> arr) {
List<Integer> list = new ArrayList<>(Collections.nCopies(arr.size(), 0));
int max = 0;
int id = 0;
for (int i = 0; i < arr.size(); i++) {
int currentId = arr.get(i);
list.set(currentId, list.get(currentId) + 1);
if (max < list.get(currentId)) {
max = list.get(currentId);
id = currentId;
} else if (max == list.get(currentId) && id > currentId) {
id = currentId;
}
}
return id;
}
public static void main(String[] args) {
List<Integer> arr = new ArrayList<>(Arrays.asList(1, 1, 2, 2, 3));
BirdsMigratingTheMost bmtm = new BirdsMigratingTheMost();
int id = bmtm.getId(arr);
System.out.println("Array: " + arr);
System.out.println("Birds migrating the most: " + id);
}
}