def PrintKMostFrequentNumbers(lst, K):
dictionary = {}
size = len(lst)
for i in range(size):
if lst[i] in dictionary:
dictionary[lst[i]] += 1
else:
dictionary[lst[i]] = 1
dic_sort_by_value = dict(sorted(dictionary.items(), key=lambda item: item[1], reverse=True))
for i in range(K):
print(list(dic_sort_by_value)[i], end=" ")
lst = [4, 5, 19, 50, 7, 19, 8, 19, 3, 3, 6, 3, 27, 19, 3, 3]
K = 2
PrintKMostFrequentNumbers(lst, K)
'''
run:
3 19
'''