def topKFrequent(nums, k): """ :type nums: List[int] :type k: int :rtype: List[int] """ frequency = {} elements = [] for x in nums: if x not in frequency: frequency[x] = 1 else: frequency[x] += 1 for y in frequency: if frequency[y] >= k: elements.append(y) return elements print(topKFrequent([1,1,1,2,2,3], 2))