Sort by

recency

|

3625 Discussions

|

  • + 0 comments

    Here is my simple C++ solution,

    int migratoryBirds(vector arr) {

    int cnt = 0;
    int prevCnt = 0;
    int res  = 0;
    for( int i =1; i <=5; i++)  // if you read the constraint it says only 1 - 5 types guranteed, so we find the max of each element and the corresponding min
    {
        cnt = std::count (arr.begin(), arr.end(), i);
    
        if( prevCnt < cnt )
        {
            prevCnt = cnt;
            res = i;
        }
        else if( prevCnt == cnt )
        {
            res = min(res, i); // here we get the min of the max cnt if they are the same
        }
    }
    return res;   
    

    }

  • + 0 comments

    Here is my Python solution

    def migratoryBirds(arr):
        # Write your code here
        
        min_number = min(arr)
        max_number = max(arr)
    
        # Find the lowest type id of the most frequently sighted birds
        lowestId = max_number
        for i in range(max_number-1, min_number, -1):
            x = arr.count(i)
            if x >= (arr.count(lowestId)):
                lowestId = i
        return lowestId
    
  • + 0 comments

    def migratoryBirds(arr) freq = {}

    # Step 1: Build frequency hash manually
    for i in arr
      if freq.has_key?(i)
        freq[i] += 1
      else
        freq[i] = 1
      end
    end
    
    # Step 2: Find the highest frequency
    max_freq = 0
    for key in freq.keys
      if freq[key] > max_freq
        max_freq = freq[key]
      end
    end
    
    # Step 3: Among all bird types with max frequency, find the smallest ID
    min_id = nil
    for key in freq.keys
      if freq[key] == max_freq
        if min_id.nil? || key < min_id
          min_id = key
        end
      end
    end
    
    return min_id
    

    end

  • + 0 comments

    Python

    def migratoryBirds(arr): # Write your code here res = [] for i in range(5): res.append(arr.count(i+1)) return res.index(max(res))+1

  • + 0 comments

    Why can't I use statistics.mode(arr) to solve it.