Sort by

recency

|

3604 Discussions

|

  • + 0 comments

    TypeScript solution O(n) by using map

    function migratoryBirds(arr: number[]): number {
        let map = new Map();
        let maxCount = 0;
        let mostFriquentBird = Number.MAX_SAFE_INTEGER;
        
        for (let bird of arr) {
            let count = (map.get(bird) || 0) + 1;
            map.set(bird, count);
            
            if (maxCount < count || (maxCount === count && bird < mostFriquentBird)) {
                maxCount = count;
                mostFriquentBird = bird;
            }
        }
        
        return mostFriquentBird;
    }
    
  • + 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

    Here is my O(n) solution using Java 8 HashMaps

    public static int migratoryBirds(List<Integer> arr) {
            Map<Integer, Integer> map = new HashMap<>();
            int count;      // counter for number of sightings
            int max = 0;    // to track the highest number of sightings
            int minID = Integer.MAX_VALUE; // to track the smallest ID
            
            for(int i = 0; i < arr.size(); i++) {
                count = map.getOrDefault(arr.get(i), 0) + 1; // get the current num of sightings for the ID and increment, defaults as 1 if the ID is not present in the map.
                map.put(arr.get(i), count); //update the number of sightings using ID as key
                if(max < count) {   // if the highest num of sightings is less than the current num of sightings  
                    max = count;    // update max num of sightings. 
                    minID = arr.get(i); // update the smallest ID
                } else if(count == max && arr.get(i) < minID) { // of the current num of sightings == max numbers of sightings AND the current ID is less than the minimum ID
                    minID = arr.get(i); // update the smallest ID only. 
                }
            }
            return minID;
        }
    
  • + 0 comments
    def migratoryBirds(arr):
        # Write your code here
        
        mydict = {}
        
        for i in arr:
            if i in mydict:
                mydict[i] += 1
            else:
                mydict[i] = 1
                
        max_count = max(mydict.values())
        
        req_list = [key for key, value in mydict.items() if value == max_count]
        req_list.sort()
        
        return req_list[0]
    
  • + 0 comments

    C# solution

    public static int migratoryBirds(List<int> arr)
        {
            int[] newArr = new int[arr.Count];
            arr.ForEach(bird => newArr[bird]++);
            
            return Array.IndexOf(newArr, newArr.Max());
        }