Top 2 most common elements inside array in Python I got this interview problem from leetcode for top k frequent elements, https://leetcode.com/problems/top-k-frequent-elements/description/ But I decide to simplify the problem just most common elements to make it simpler to solve.  If the 3 item has the same frequentcy, then it's okay to return all 3.

"""Given an array, return an array of the top 2 most elements. // Input: [1, 2, 3, 4, 5, 3, 4] | Output: [3, 4] // Input: [2, 2, 3, 2, 4, 3, 4] | Output: [2,4]"""   

import collections

def top2Frequent(nums):
    """
    :type nums: List[int]
    :type k: int
    :rtype: List[int]
    """
    if len(nums) &lt; 2:
        return []
    freq = {}
    for num in nums:
        if num in freq:
            freq[num] = freq[num] + 1
        else:
            freq[num] = 1

    bucket = collections.defaultdict(list)
    for key in freq:
        f = freq[key]
        bucket[f].append(key)
    res = []
    count = len(nums)  # the upper limit for res
    while len(res) &lt; 2:
        if bucket[count]:
            res += bucket[count]
        count -= 1
    return res


nums = [1, 4, 3, 4, 5, 3, 4]  # [4,3]
solution = top2Frequent(nums)
print(solution) # [4,3]


https://gist.github.com/Jeffchiucp/2e733e57476bd697bc430cdf48f6e180

I am also tried to solve this problem without using any python library built in collections.
If you're okay with using the built-in collections module, I'd suggest the following. collections.Counter is ideally suited for this and will make the problem trivial.

from collections import Counter


def top_k(numbers, k=2):
    """The counter.most_common([k]) method works
    in the following way:
    &gt;&gt;&gt; Counter('abracadabra').most_common(3)  
    [('a', 5), ('r', 2), ('b', 2)]
    """

    c = Counter(numbers)
    most_common = [key for key, val in c.most_common(k)]

    return most_common


Without using the built-in collections module, the following will work:

def top_k_nobuiltin(numbers, k=2):
    # The first part is identical to your implementation and works just
    # fine
    counter = {}
    for number in numbers:
        if number in counter:
            counter[number] += 1
        else:
            counter[number] = 1

    # The line below creates an iterator that will generate the
    # sequence [(4, 3), (3, 2), (5, 1), (1, 1)], i.e.
    # count the occurrence for each value.
    # In particular, sorted() with key=lambda kv: kv[1] will turn
    # a dictionary into a list of tuples, sorted by the second item
    # of each tuple

    sorted_by_value = reversed(sorted(counter.items(), key=lambda kv: kv[1]))

    top_vals = [item[0] for item in sorted_by_value][:k]

    return top_vals 


For both cases, you'll get the desired result:

def main():
    nums = [1, 4, 3, 4, 5, 3, 4]  # [4,3]

    print(top_k(nums))  # [4,3]
    print(top_k_nobuiltin(nums))  # [4,3]    

if __name__ == '__main__':
    main()