Migratory Birds

Sort by

recency

|

191 Discussions

|

  • + 0 comments

    C#

    public static int migratoryBirds(List<int> arr)
        {
            int mostFreq = 0;
            int id = 0;
            
            arr.GroupBy(id => id)
                .ToList()
                .ForEach(g => {
                   Console.WriteLine(g.ToList().Count());
                   int birsFre = g.ToList().Count();
                   int currentId = g.First();
                   if(birsFre > mostFreq) {
                        mostFreq = birsFre;
                        id = currentId; 
                   }
                   if(birsFre == mostFreq){
                        id = id < currentId ? id : currentId;
                   }
                });
                
            return id;
        }
    
  • + 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