Migratory Birds

Sort by

recency

|

187 Discussions

|

  • + 0 comments
    def migratoryBirds(arr):
        # Write your code here
        d ={}
        for i in arr:
            if i in d.keys():
                d[i]+=1
            else:
                d[i]=1
        maxval = 0
        minkey = 0
        for key,value in d.items():
            if value>maxval:
                maxval=value
                minkey=key
            elif value==maxval:
                if key<minkey:
                    minkey=key
        return minkey
    
  • + 0 comments

    Python best solution

    If you’re looking for solutions to the 3-month preparation kit in either Python or Rust, you can find them below: my solutions

    def migratory_birds(arr):
        #Time complexity: O(n)
        #Space complexity (ignoring input): O(1). You could use a frequency array instead
        #of a dictionary and use even less space
        birds_dict = {}
        for bird in arr:
            if bird in birds_dict:
                birds_dict[bird]+=1
            else:
                birds_dict[bird]=1
    
        most_frequent = [6,0]
        for (bird, frequency) in birds_dict.items():
            if frequency > most_frequent[1]:
                most_frequent = [bird, frequency]
            if (frequency == most_frequent[1]) and (bird < most_frequent[0]):
                most_frequent[0] = bird
    
        return most_frequent[0]
    
  • + 1 comment

    Rust best solution

    If you’re looking for solutions to the 3-month preparation kit in either Python or Rust, you can find them below: my solutions

    fn migratory_birds(arr: &[i32]) -> i32 {
        //Time complexity: O(n)
        //Space complexity (ignoring input): O(1). You could use a frequency array instead
        //of a dictionary and use even less space
        let mut birds_hash = std::collections::HashMap::new();
        for &bird in arr {
            match birds_hash.get(&bird) {
                Some(value) => birds_hash.insert(bird, value + 1),
                None => birds_hash.insert(bird, 1),
            };
        }
    
        struct BirdInfo {
            bird: i32,
            frequency: i32,
        }
        let mut most_frequent = BirdInfo {
            bird: 6,
            frequency: 0,
        };
        for (bird, frequency) in birds_hash.iter() {
            if frequency > &most_frequent.frequency {
                most_frequent.bird = *bird;
                most_frequent.frequency = *frequency
            }
            if (frequency == &most_frequent.frequency) & (bird < &most_frequent.bird) {
                most_frequent.bird = *bird;
            }
        }
        most_frequent.bird
    }
    
  • + 0 comments
    #Python Solution
    def migratoryBirds(arr):
        birds = {bird:arr.count(bird) for bird in set(arr)}
        return min(int(bird) for bird, count in birds.items() if count == max(map(int,birds.values())))
    
  • + 0 comments

    Java solution

    public static int migratoryBirds(List<Integer> arr) {
            var groups = arr.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
            return groups.entrySet().stream().max(Map.Entry.comparingByValue()).map(Map.Entry::getKey).orElse(0);
        }