Migratory Birds

Sort by

recency

|

181 Discussions

|

  • + 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
    
  • + 1 comment

    // couldn't find better 1 at the time

    def migratoryBirds(arr):

    n_arr = []
    for i in set(arr):
        k = 0
        for j in arr:
            if i == j:
                k += 1
                n_arr.append((i, k))
    
    m = max([i[1] for i in n_arr if i[1]])
    return min([i[0] for i in n_arr if i[1] >= m])