We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
public class MigratoryBirds{
public static void main(String[] args) throws IOException {
System.out.println(migratoryBirds());
}
//Worst Case: O(n)
static int migratoryBirds() throws IOException {
Reader reader = new Reader();
int n = reader.readInt();
final int k = 6;
int[] counts = new int[k];
int type = 0;
int maxCount = 0;
for (int i = 0; i < n; i++) {
int birdType = reader.readInt();
counts[birdType]++;
if(counts[birdType] > maxCount ||
(counts[birdType] == maxCount && birdType < type)) {
type = birdType;
maxCount = counts[birdType];
}
}
return type;
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Migratory Birds
You are viewing a single comment's thread. Return to all comments →
Best Solution in O(n) time:
import java.io.IOException;
public class MigratoryBirds{ public static void main(String[] args) throws IOException { System.out.println(migratoryBirds()); }
}