• + 0 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()); }

    //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;
    }
    

    }