Migratory Birds

Sort by

recency

|

182 Discussions

|

  • + 0 comments

    Pyhton3 solution

    def migratoryBirds(arr):
        # Write your code here
        temp = [0]*len(arr)
        for i in arr:
            temp[i] += 1
        return temp.index(max(temp))
    
  • + 0 comments

    Python 3 solun using collections.Counter

    from collections import Counter
    
    def migratoryBirds(arr):
        return min([bird for bird, count in Counter(arr).items() if count == max(Counter(arr).values())])
    

    overall time complexity is O(N)

  • + 0 comments

    Rust solution:

    fn birds(arr: &[i32]) -> i32 {
        let mut map = HashMap::new();
    
        for &i in arr {
            *map.entry(i).or_insert(0) += 1;
        }
    
        *map.iter().max_by_key(|(&id, &n)| (n, -id)).unwrap().0
    }
    
  • + 0 comments

    Python 1 liner: O(N)

    return max(Counter(arr).items(), key= lambda i: 10*i[1] + 5-i[0])[0]
    
  • + 0 comments
    emp_arr = [0]*(max(arr)+1)
    for i in arr:
        emp_arr[i] = emp_arr[i] + 1
    for i in range(len(emp_arr)):
        if emp_arr[i] == max(emp_arr):
            return i