Sort by

recency

|

3621 Discussions

|

  • + 0 comments

    Why can't I use statistics.mode(arr) to solve it.

  • + 0 comments
    public static int migratoryBirds(List<Integer> arr) {
        Comparator<Long> longComp = Long::compare;
        Map<Integer, Long> countMap = arr.stream().collect(Collectors.groupingBy((Integer i)->i, Collectors.counting()));
        long maxFrequency = countMap.entrySet().stream().sorted(Map.Entry.comparingByValue(longComp.reversed())).
        findFirst().get().getValue();
        Integer key = countMap.entrySet().stream().filter(en -> en.getValue() == maxFrequency).sorted(Map.Entry.comparingByKey()).findFirst().get().getKey();
        return key;
    }   
    
  • + 0 comments

    Here is a python solution with O(n + k) time and O(k) space:

    def migratoryBirds(arr):
        count = {}
        for n in arr:
            if n not in count:
                count[n] = 0
            count[n] += 1
            
        maxval = max(count.values())
        
        best = []
        for key, val in count.items():
            if val == maxval:
                best.append(key)             
    
        return min(best)
    
  • + 0 comments

    Here is problem solution in Python, Java, C++, C and Javascript - https://programmingoneonone.com/hackerrank-migratory-birds-problem-solution.html

  • + 0 comments

    n = int(input()) arr = list(map(int(input().strip().split(" ")))) d={} for i in arr: d[i] = arr.count(i)

    maxfreq = max(d.values())
    result = 10000 for i in d: if d[i] == maxfreq: result = min(i,result) print(result)