Migratory Birds

Sort by

recency

|

190 Discussions

|

  • + 1 comment

    fun migratoryBirds(arr: Array): Int {

    val birdMap = mutableMapOf<Int, Int>()
    for (bird in arr) {
        birdMap[bird] = birdMap.getOrDefault(bird, 0) + 1
    }
    
    val value = birdMap.map { it.value }.max()
    val id = birdMap.filterValues { it == value }.keys.min()
    
    return id
    

    }

  • + 0 comments
    public static int migratoryBirds(List<Integer> arr) {
    // Write your code here
        Map<Integer,Integer> countMap = new HashMap<>();
        List<Integer> maxList = new ArrayList<>();
        for(Integer i : arr){
            countMap.put(i, countMap.getOrDefault(i, 0) + 1);
        }
    
        Integer max = Collections.max(countMap.values());
        for(Map.Entry<Integer, Integer> maps : countMap.entrySet() ){
            if(maps.getValue() == max){
                maxList.add(maps.getKey());
            }
        }
        return Collections.min(maxList);
    }
    
  • + 0 comments
    frequency = {}
        for i in arr:
            frequency[i] = frequency.get(i,0) + 1
        maxCount = 0
        bird = None
        for key, values in frequency.items():
            if values > maxCount:
                maxCount = values
                bird = key  
            if values == maxCount and key < bird:
                bird = key  
        return bird
    
  • + 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]