Sort by

recency

|

3588 Discussions

|

  • + 0 comments

    Here is my c++ solution you can find the explanation here : https://youtu.be/poQPrHTMe08

    int migratoryBirds(vector<int> arr) {
        map<int, int> mp;
        int cnt = 0, id;
        for(int i = 0; i < arr.size(); i++)mp[arr[i]]++;
        for(auto it = mp.begin(); it != mp.end(); it++){
            if(it->second > cnt || (it->second == cnt && it->first < id)){
                cnt = it->second;
                id = it->first;
            }
        }
        return id;
    }
    
  • + 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;
    }
    

    }

  • + 1 comment

    PHP easy solution Migratory Birds

    function migratoryBirds($arr) {
        $counts = array_count_values($arr);
        ksort($counts);
        arsort($counts);
        return array_key_first($counts);
    }
    
  • + 0 comments

    Python 3: Used a key/value pair to map out each value and increment the times the id has been seen like some sort of counter.

    def migratoryBirds(arr):
        integer_dictionary = {}
        for number in arr:
            ## To prevent an issue where the key doesn't exist in our dictionary
            ## before an addtion assignment "+=" we check whether or not to initialize it.
            if number in integer_dictionary:
                integer_dictionary[number] += 1
            else:
                integer_dictionary[number] = 1
        ## Now we find the upmost maximum value.
        maximum_value = max(integer_dictionary.values())
        
        ## And we use that to find our smallest id if there are multiple
        ## high level values.
        keys_with_maximum_values = []
        for key, value in integer_dictionary.items():
            if value == maximum_value:
                keys_with_maximum_values.append(key)
        
        ## Returning the smallest id found.
        return min(keys_with_maximum_values)
    
  • + 0 comments

    Java

    public static int migratoryBirds(List<Integer> arr) {
        // Write your code here
        
            if(arr == null || arr.isEmpty()) return 0;
            if(arr.size() == 1) return arr.get(0);
    
            Map<Integer, Integer> counterMap = new HashMap<>();
            int maxFrequency = 0;
            int result = Integer.MAX_VALUE;
            for(Integer bird : arr){
                counterMap.put(bird, counterMap.getOrDefault(bird, 0) + 1);
                Integer frequency = counterMap.get(bird);
                if(frequency > maxFrequency){
                    maxFrequency = frequency;
                    result = bird;
                }
    
                if(frequency == maxFrequency){
                    result = Math.min(result, bird);
                }
            }
    
        return result;
    
    }