Migratory Birds

  • + 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]