Given a non-empty array of integers, find the top k elements which have the highest frequency in the array. If two numbers have the same frequency then the larger number should be given preference. 
Note: Print the elements according to the frequency count (from highest to lowest) and if the frequency is equal then larger number will be given preference.
Example 1:
Input:
N = 6
nums = {1,1,1,2,2,3}
k = 2
Output: {1, 2}
Example 2:
Input:
N = 8
nums = {1,1,2,2,3,3,3,4}
k = 2
Output: {3, 2}
Explanation: Elements 1 and 2 have the
same frequency ie. 2. Therefore, in this
case, the answer includes the element 2
as 2 > 1.
User Task:
The task is to complete the function topK() that takes the array and integer k as input and returns a list of top k frequent elements.
Expected Time Complexity : O(NlogN)
Expected Auxilliary Space : O(N)
Constraints: 
1 <= N <= 10^{5}
1<=A[i]<=10^{5}

Difficulty: EASY
Tags: String algorithms

Sorting

Data structures
Skill types: Sorting

Data structures
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(N)

Starter code:
class Solution:

	def topK(self, nums, k):

		#Code here

Solutions:
class Solution:

	def topK(self, nums, k):
		d = {}
		(max, min) = (1, 1)
		for i in nums:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
		return sorted(d, key=lambda i: (d[i], i), reverse=True)[:k]


class Solution:

	def topK(self, nums, k):
		mydict = {}
		for i in nums:
			mydict[i] = mydict.get(i, 0) + 1
		arr = [0] * len(mydict)
		j = 0
		for i in mydict:
			arr[j] = [i, mydict[i]]
			j += 1
		arr = sorted(arr, key=lambda x: x[0], reverse=True)
		arr = sorted(arr, key=lambda x: x[1], reverse=True)
		a = [i[0] for i in arr[0:k]]
		return a


class Solution:

	def topK(self, nums, k):
		frequency_dict = {}
		for num in nums:
			frequency_dict[num] = frequency_dict.get(num, 0) + 1
		sorted_nums = sorted(frequency_dict, key=lambda x: (-frequency_dict[x], -x))
		return sorted_nums[:k]


class Solution:

	def topK(self, nums, k):
		d = {}
		for i in range(len(nums)):
			if nums[i] not in d:
				d[nums[i]] = 1
			else:
				d[nums[i]] += 1
		sortedNums = sorted(d, key=lambda item: (d[item], item), reverse=True)[:k]
		return sortedNums


class Solution:

	def topK(self, nums, k):
		temp = dict()
		for i in nums:
			if i in temp:
				temp[i] += 1
			else:
				temp[i] = 1
		return sorted(temp, key=lambda item: (temp[item], item), reverse=True)[:k]


from collections import Counter

class Solution:

	def topK(self, nums, k):
		y = Counter(nums)
		z = sorted(y.items(), key=lambda x: (x[1], x[0]), reverse=True)
		l = []
		for i in range(k):
			l.append(z[i][0])
		return l


class Solution:

	def topK(self, nums, k):
		d = {}
		for i in range(len(nums)):
			if nums[i] in d:
				d[nums[i]] += 1
			else:
				d[nums[i]] = 1
		max = sorted(d.items(), key=lambda x: (-x[1], -x[0]))
		l = []
		for i in range(k):
			l.append(max[i][0])
		return l


import heapq

class Solution:

	def topK(self, nums, k):
		freq_map = {}
		for num in nums:
			freq_map[num] = freq_map.get(num, 0) + 1
		arr = []
		for (d, v) in freq_map.items():
			arr.append((d, v))
		arr.sort(key=lambda x: (-x[1], -x[0]))
		ans = [arr[i][0] if i < len(arr) else 0 for i in range(k)]
		return ans


class Solution:

	def topK(self, nums, k):
		mp = {}
		arr = nums
		for i in range(n):
			if arr[i] in mp:
				mp[arr[i]] += 1
			else:
				mp[arr[i]] = 1
		a = [0] * len(mp)
		j = 0
		for i in mp:
			a[j] = [i, mp[i]]
			j += 1
		a = sorted(a, key=lambda x: x[0], reverse=True)
		a = sorted(a, key=lambda x: x[1], reverse=True)
		l = []
		for i in range(k):
			l.append(a[i][0])
		return l


class Solution:

	def topK(self, nums, k):
		mp = {}
		for i in range(n):
			if nums[i] in mp:
				mp[nums[i]] += 1
			else:
				mp[nums[i]] = 1
		mp = sorted(mp.items(), key=lambda x: (-x[1], -x[0]))
		l = []
		for i in range(k):
			l.append(mp[i][0])
		return l


from collections import Counter

class Solution:

	def topK(self, nums, k):
		freq_dict = Counter(nums)
		sorted_elements = sorted(freq_dict.keys(), key=lambda x: (-freq_dict[x], -x))
		top_k_elements = sorted_elements[:k]
		return top_k_elements


class Solution:

	def topK(self, nums, k):
		l = []
		d = dict()
		for i in nums:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		p = len(d)
		sorted_d = sorted(d.items(), key=lambda x: (-x[1], -x[0]))
		for i in range(k):
			l.append(sorted_d[i][0])
		return l


class Solution:

	def topK(self, nums, k):
		um = {}
		n = len(nums)
		for i in range(n):
			if nums[i] in um:
				um[nums[i]] += 1
			else:
				um[nums[i]] = 1
		a = [0] * len(um)
		j = 0
		for i in um:
			a[j] = [i, um[i]]
			j += 1
		a = sorted(a, key=lambda x: x[0], reverse=True)
		a = sorted(a, key=lambda x: x[1], reverse=True)
		res = []
		for i in range(k):
			res.append(a[i][0])
		return res


class Solution:

	def topK(self, nums, k):
		d = {}
		for i in nums:
			d[i] = d.get(i, 0) + 1
		res1 = sorted([[d[i], i] for i in d])[-k:][::-1]
		res = []
		for i in res1:
			res.append(i[1])
		return res


class Solution:

	def topK(self, nums, k):
		d = {}
		for i in nums:
			d[i] = d.get(i, 0) + 1
		res1 = sorted(d.items(), key=lambda x: (x[1], x[0]), reverse=True)
		res = []
		for i in range(k):
			res.append(res1[i][0])
		return res


class Solution:

	def topK(self, nums, k):
		d = {}
		for i in nums:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
		l = sorted([[d[i], i] for i in d])[-k:]
		k = [j for (i, j) in l][::-1]
		res = []
		for i in k:
			if i in d:
				res.append(i)
		return res


class Solution:

	def topK(self, nums, k):
		count = {}
		l = []
		if len(nums) <= 1:
			return [nums[0]]
		else:
			for i in range(len(nums)):
				if nums[i] in count.keys():
					count[nums[i]] += 1
				else:
					count[nums[i]] = 1
			sorted_f = sorted(count.items(), key=lambda x: (-x[1], -x[0]))
			l = []
			for i in range(k):
				l.append(sorted_f[i][0])
			return l
			return sorted_f


class Solution:

	def topK(self, nums, k):
		dic = {}
		for ele in nums:
			dic[ele] = dic.get(ele, 0) + 1
		dic = sorted(dic.items(), key=lambda x: (x[1], x[0]), reverse=True)
		ans = []
		for o in range(k):
			ans.append(dic[o][0])
		return ans


import collections
from collections import *

class Solution:

	def topK(self, nums, k):
		arr = Counter(nums)
		ke = list(arr.keys())
		val = list(arr.values())
		lst = []
		for i in range(len(ke)):
			lst.append([val[i], ke[i]])
		lst = sorted(lst, reverse=True)
		ans = []
		for i in range(k):
			ans.append(lst[i][1])
		return ans


class Solution:

	def topK(self, nums, k):
		n = len(nums)
		a = [[] for i in range(n + 1)]
		dic = {}
		for i in nums:
			if i not in dic:
				dic[i] = 1
			else:
				dic[i] += 1
		for (key, v) in dic.items():
			a[v].append(key)
		res = []
		for i in reversed(a):
			i.sort()
			for n in reversed(i):
				res.append(n)
				if len(res) == k:
					return res
		return res


class Solution:

	def topK(self, nums, k):
		frequency = {}
		for i in nums:
			if i in frequency:
				frequency[i] = frequency[i] + 1
			else:
				frequency[i] = 1
		sorted_f = sorted(frequency.items(), key=lambda x: (-x[1], -x[0]))
		ls = []
		for i in range(k):
			ls.append(sorted_f[i][0])
		return ls


class Solution:

	def topK(self, nums, k):
		a = max(nums)
		arr_f = [0] * (a + 1)
		for i in nums:
			arr_f[i] = arr_f[i] + 1
		for i in range(len(arr_f)):
			arr_f[i] = (arr_f[i], i)
		arr_f = sorted(arr_f, key=lambda x: (x[0], x[1]), reverse=True)
		lst = []
		for i in arr_f:
			lst.append(i[1])
		return lst[:k]


class Solution:

	def topK(self, nums, k):
		dict = {}
		for i in nums:
			dict[i] = dict.get(i, 0) + 1
		result = []
		for i in dict:
			result.append((i, dict[i]))
		result.sort(key=lambda x: (x[1], x[0]), reverse=True)
		q = []
		for i in range(k):
			q.append(result[i][0])
		return q


class Solution:

	def topK(self, nums, k):
		hash_dict = {}
		for i in range(len(nums)):
			if nums[i] in hash_dict:
				hash_dict[nums[i]] += 1
			else:
				hash_dict[nums[i]] = 1
		temp = sorted(hash_dict.items(), key=lambda v: (v[1], v[0]))[::-1]
		res = []
		for i in range(k):
			res.append(temp[i][0])
		return res


class Solution:

	def topK(self, nums, k):
		dict = {}
		for x in nums:
			if x not in dict:
				dict[x] = 0
			else:
				dict[x] += 1
		dict2 = sorted(dict.items(), key=lambda x: (-x[1], -x[0]))
		li = []
		for i in range(k):
			li.append(dict2[i][0])
		return li


class Solution:

	def topK(self, nums, k):
		map = {}
		for num in nums:
			if num in map:
				map[num] += 1
			else:
				map[num] = 1
		items = list(map.items())
		items = sorted(items, key=lambda x: (x[1], x[0]), reverse=True)
		ans = []
		for i in range(k):
			ans.append(items[i][0])


from collections import Counter

class Solution:

	def topK(self, nums, k):
		if len(nums) < 1:
			return [nums[0]]
		dic = Counter(nums)
		sorted_dic = sorted(dic.items(), key=lambda x: (-x[1], -x[0]))
		ans = []
		for i in range(k):
			ans.append(sorted_dic[i][0])
		return ans


class Solution:

	def topK(self, nums, k):
		d = {}
		for i in nums:
			if i not in d:
				d[i] = 1
			else:
				d[i] = d[i] + 1
		res = []
		for (i, j) in sorted(d.items(), key=lambda x: (x[1], x[0]), reverse=True):
			res.append(i)
			k = k - 1
			if k == 0:
				break
		return res


class Solution:

	def topK(self, nums, k):
		freq_dict = {}
		for num in nums:
			if num not in freq_dict:
				freq_dict[num] = 0
			freq_dict[num] += 1
		sorted_nums = sorted(nums, reverse=True)
		freq_list = []
		for num in set(sorted_nums):
			freq_list.append((num, freq_dict[num]))
		freq_list.sort(key=lambda x: (-x[1], -x[0]))
		return [x[0] for x in freq_list[:k]]


from collections import defaultdict

class Solution:

	def topK(self, nums, k):
		freq_dict = defaultdict(int)
		for num in nums:
			freq_dict[num] += 1
		sorted_nums = sorted(nums, reverse=True)
		freq_list = []
		for num in set(sorted_nums):
			freq_list.append((num, freq_dict[num]))
		freq_list.sort(key=lambda x: (-x[1], -x[0]))
		return [x[0] for x in freq_list[:k]]


from collections import Counter
import heapq

class Solution:

	def topK(self, nums, k):
		a = Counter(nums)
		ans = []
		h = [[-a[i], -i] for i in a]
		heapq.heapify(h)
		for _ in range(k):
			x = heapq.heappop(h)
			ans.append(-x[1])
		return ans


class Solution:

	def topK(self, nums, k):
		d = {}
		for i in nums:
			try:
				d[i] += 1
			except:
				d[i] = 1
		d = sorted(d.items(), key=lambda x: (x[1], x[0]), reverse=True)
		l = []
		for i in d:
			l.append(i[0])
			if len(l) == k:
				return l


class Solution:

	def topK(self, nums, k):

		def compare(x, y):
			if x[0] != y[0]:
				return x[0] < y[0]
			return x[1] < y[1]
		temp = {}
		for num in nums:
			temp[num] = temp.get(num, 0) + 1
		res = []
		for (_, num) in sorted([[v, k] for (k, v) in temp.items()])[::-1]:
			if k == 0:
				break
			res.append(num)
			k -= 1
		return res


class Solution:

	def topK(self, nums, k):
		count = {}
		for i in nums:
			if i in count:
				count[i] += 1
			else:
				count[i] = 1
		a = [0] * len(count)
		j = 0
		for i in count:
			a[j] = [i, count[i]]
			j += 1
		a = sorted(a, key=lambda x: x[0], reverse=True)
		a = sorted(a, key=lambda x: x[1], reverse=True)
		ans = []
		for i in range(k):
			ans.append(a[i][0])
		return ans


class Solution:

	def topK(self, nums, k):
		hmap = {}
		for val in nums:
			hmap[val] = hmap.get(val, 0) + 1
		a = [0] * len(hmap)
		j = 0
		for i in hmap:
			a[j] = [i, hmap[i]]
			j += 1
		a = sorted(a, key=lambda x: x[0], reverse=True)
		a = sorted(a, key=lambda x: x[1], reverse=True)
		ans = []
		for i in range(k):
			ans.append(a[i][0])
		return ans


class Solution:

	def topK(self, nums, k):
		freq_dict = {}
		for num in nums:
			if num in freq_dict:
				freq_dict[num] += 1
			else:
				freq_dict[num] = 1
		freq_list = [(freq, num) for (num, freq) in freq_dict.items()]
		freq_list.sort(reverse=True)
		topk = [num for (freq, num) in freq_list[:k]]
		return topk


class Solution:

	def topK(self, nums, k):
		dic = {}
		for i in range(len(nums)):
			dic[nums[i]] = dic.get(nums[i], 0)
			dic[nums[i]] += 1
		temp = [0] * len(dic)
		j = 0
		for i in dic:
			temp[j] = [i, dic[i]]
			j += 1
		temp = sorted(temp, key=lambda x: x[0], reverse=True)
		temp = sorted(temp, key=lambda x: x[1], reverse=True)
		res = []
		for i in range(k):
			res.append(temp[i][0])
		return res


import collections

class Solution:

	def topK(self, nums, k):
		store = collections.Counter(nums)
		return sorted(store, key=lambda x: (store[x], x), reverse=True)[:k]


import collections

class Solution:

	def topK(self, nums, kz):
		a = collections.Counter(nums)
		(k, v) = (list(a.keys()), list(a.values()))
		res = []
		for i in range(len(k)):
			res.append([v[i], k[i]])
		res.sort(reverse=True)
		s = []
		for i in range(kz):
			s.append(res[i][1])
		return s


import heapq

class Solution:

	def topK(self, nums, k):
		mp = {}
		for n in nums:
			mp[n] = mp[n] + 1 if n in mp else 1
		tt = [(-mp[x], -x) for x in mp]
		heapq.heapify(tt)
		k = min(k, len(tt))
		return [-heapq.heappop(tt)[1] for _ in range(k)]


class Solution:

	def topK(self, nums, k):
		mp = {}
		for n in nums:
			mp[n] = mp[n] + 1 if n in mp else 1
		tt = [(mp[x], x) for x in mp]
		tt.sort(reverse=True)
		k = min(k, len(tt))
		return [tt[i][1] for i in range(k)]


class Solution:

	def topK(self, nums, k):
		d = {}
		lst = []
		for i in range(0, len(nums)):
			if nums[i] in d:
				d[nums[i]] += 1
			else:
				d[nums[i]] = 1
		keys = list(d.keys())
		vals = list(d.values())
		for i in range(0, len(keys)):
			lst.append([vals[i], keys[i]])
		lst.sort(reverse=True)
		op = []
		for i in range(k):
			op.append(lst[i][1])
		return op


class Solution:

	def topK(self, l, k):
		h = {}
		for i in l:
			if i not in h:
				h[i] = 1
			else:
				h[i] = h[i] + 1
		return sorted(h, key=lambda item: (h[item], item), reverse=True)[:k]


from heapq import nlargest

class Solution:

	def topK(self, nums, k):
		counter = {}
		for i in nums:
			if i not in counter:
				counter[i] = 1
			else:
				counter[i] += 1
		lst = [(val, key) for (key, val) in counter.items()]
		large = nlargest(k, lst)
		ans = []
		for i in range(k):
			ans.append(large[i][1])
		return ans


import heapq

class Solution:

	def topK(self, nums, k):
		mp = {}
		for i in nums:
			if i in mp:
				mp[i] += 1
			else:
				mp[i] = 1
		heap = []
		for i in mp:
			heapq.heappush(heap, [mp[i], i])
			if len(heap) > k:
				heapq.heappop(heap)
		ans = []
		while len(heap):
			ans.append(heapq.heappop(heap)[1])
		ans.reverse()
		return ans


class Solution:

	def topK(self, arr, K):
		N = len(arr)
		mp = {}
		for i in range(N):
			if arr[i] in mp:
				mp[arr[i]] += 1
			else:
				mp[arr[i]] = 1
		a = [0] * len(mp)
		j = 0
		for i in mp:
			a[j] = [i, mp[i]]
			j += 1
		a = sorted(a, key=lambda x: x[0], reverse=True)
		a = sorted(a, key=lambda x: x[1], reverse=True)
		for i in range(K):
			print(a[i][0], end=' ')
		return []


class Solution:

	def topK(self, nums, k):
		re = {}
		for e in nums:
			if e not in re.keys():
				re[e] = 1
			else:
				re[e] = re[e] + 1
		res = sorted(re.items(), key=lambda x: x[0])
		res = sorted(res, key=lambda x: x[1])[-k:]
		r = [res[i - 1][0] for i in range(len(res), 0, -1)]
		return r


class Solution:

	def topK(self, nums, k):
		store = {}
		for num in nums:
			if num in store:
				store[num] += 1
			else:
				store[num] = 1
		a = [0] * len(store)
		j = 0
		for i in store:
			a[j] = [i, store[i]]
			j += 1
		store = sorted(store.items(), key=lambda x: x[0], reverse=True)
		store = sorted(store, key=lambda x: x[1], reverse=True)
		res = []
		for i in range(k):
			res.append(store[i][0])
		return res


import heapq
from collections import Counter

class Solution:

	def topK(self, nums, k):
		d = Counter(nums)
		heap = []
		for (i, j) in d.items():
			heapq.heappush(heap, [-j, -i])
		ans = []
		for i in range(k):
			(freq, ele) = heapq.heappop(heap)
			ans.append(-1 * ele)
		return ans


from collections import Counter

class Solution:

	def topK(self, nums, k):
		d = Counter(nums)
		a = []
		for (i, j) in d.items():
			a.append([j, i])
		a.sort(reverse=True)
		ans = []
		for i in range(k):
			ans.append(a[i][1])
		return ans


class Solution:

	def topK(self, nums, k):
		d = dict()
		for i in nums:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		a = []
		for (i, j) in d.items():
			a.append([j, i])
		a.sort(reverse=True)
		ans = []
		for i in range(k):
			ans.append(a[i][1])
		return ans


class Solution:

	def topK(self, nums, k):
		freq = {}
		for i in nums:
			freq[i] = freq.get(i, 0) + 1
		arr = list(freq.items())
		arr = sorted(arr, key=lambda x: (x[1], x[0]), reverse=True)
		arr = [i[0] for i in arr]
		return arr[:k]


from collections import Counter, defaultdict

class Solution:

	def topK(self, nums, k):
		arr = Counter(nums)
		return list(zip(*sorted(zip(arr.values(), arr.keys()), reverse=True)))[1][:k]


import heapq

class Solution:

	def topK(self, arr, K):
		mp = dict()
		N = len(arr)
		for i in range(0, N):
			if arr[i] not in mp:
				mp[arr[i]] = 0
			else:
				mp[arr[i]] += 1
		heap = [(value, key) for (key, value) in mp.items()]
		largest = heapq.nlargest(K, heap)
		ans = []
		for i in range(K):
			ans.append(largest[i][1])
		return ans


class Solution:

	def topK(self, nums, k):
		freq = {}
		for (i, n) in enumerate(nums):
			if n in freq:
				freq[n] += 1
			else:
				freq[n] = 1
		arr = []
		for i in freq:
			arr.append([i, freq[i]])
		arr = sorted(arr, key=lambda x: x[0], reverse=True)
		arr = sorted(arr, key=lambda x: x[1], reverse=True)
		ans = []
		for i in range(k):
			ans.append(arr[i][0])
		return ans


class Solution:

	def topK(self, nums, k):
		dic = {}
		for i in nums:
			if i in dic:
				dic[i] += 1
			else:
				dic[i] = 1
		l = []
		for (i, j) in dic.items():
			l.append([j, i])
		l.sort(reverse=True)
		ans = []
		for i in range(k):
			ans.append(l[i][1])
		return ans


class Solution:

	def topK(self, nums, k):
		m1 = {}
		for i in range(len(nums)):
			if nums[i] not in m1.keys():
				m1[nums[i]] = 1
			else:
				m1[nums[i]] += 1
		a = [0] * len(m1)
		j = 0
		for i in m1:
			a[j] = [i, m1[i]]
			j += 1
		a = sorted(a, key=lambda x: x[0], reverse=True)
		a = sorted(a, key=lambda x: x[1], reverse=True)
		p = []
		for i in range(k):
			p.append(a[i][0])
		return p


class Solution:

	def topK(self, nums, k):
		freq_dict = {}
		for num in nums:
			freq_dict[num] = freq_dict.get(num, 0) + 1
		sorted_dict = sorted(freq_dict.items(), key=lambda x: (-x[1], -x[0]))
		return [item[0] for item in sorted_dict[:k]]


class Solution:

	def topK(self, nums, k):
		dct = {}
		for num in nums:
			if num in dct:
				dct[num] += 1
			else:
				dct[num] = 1
		arr = []
		res = []
		for i in dct:
			arr.append((dct[i], i))
		arr.sort(reverse=True)
		for i in range(k):
			res.append(arr[i][1])
		return res


import heapq

class Solution:

	def topK(self, nums, k):
		freq_map = {}
		for i in nums:
			if i in freq_map:
				freq_map[i] += 1
			else:
				freq_map[i] = 1
		minh = []
		heapq.heapify(minh)
		for i in freq_map:
			heapq.heappush(minh, [freq_map[i], i])
			if len(minh) > k:
				heapq.heappop(minh)
		ans = []
		while minh:
			x = heapq.heappop(minh)
			ans.append(x[1])
		ans.reverse()
		return ans


class Solution:

	def topK(self, nums, k):
		nums.sort()
		freq_map = {}
		for num in nums:
			if num in freq_map.keys():
				freq_map[num] += 1
			else:
				freq_map[num] = 1
		freq_map_2 = {}
		res = []
		num_res = 0
		for (key, value) in freq_map.items():
			if value in freq_map_2.keys():
				freq_map_2[value].append(key)
			else:
				freq_map_2[value] = [key]
		sorted_keys = list(freq_map_2.keys())
		sorted_keys.sort(reverse=True)
		while len(res) < k:
			freq_map_2[sorted_keys[num_res]].sort(reverse=True)
			res += freq_map_2[sorted_keys[num_res]]
			num_res += 1
		res = res[:k]
		return res


class Solution:

	def topK(self, nums, k):
		my_dick = {}
		for i in nums:
			if i not in my_dick:
				my_dick[i] = 1
			else:
				my_dick[i] += 1
		my_dict = sorted(my_dick.items(), key=lambda x: (x[1], x[0]), reverse=True)
		ret_arr = [0] * k
		for i in range(k):
			ret_arr[i] = my_dict[i][0]
		return ret_arr


class Solution:

	def topK(self, nums, k):
		d = {}
		for elem in nums:
			d.update({elem: d.get(elem, 0) + 1})
		res = list(d.items())
		res.sort(key=lambda x: (-x[1], -x[0]))
		return [item[0] for item in res[:k]]


import heapq

class Solution:

	def topK(self, nums, k):
		fre_count = {}
		hp = []
		for i in nums:
			fre_count[i] = 1 + fre_count.get(i, 0)
		for i in fre_count:
			heapq.heappush(hp, (fre_count[i], i))
			if len(hp) > k:
				heapq.heappop(hp)
		ans = []
		for i in range(len(hp)):
			ans.append(heapq.heappop(hp)[1])
		return ans[::-1]


from heapq import heappop, heappush

class Solution:

	def topK(self, nums, k):
		hmap = {}
		for i in range(len(nums)):
			if nums[i] in hmap:
				hmap[nums[i]] += 1
			else:
				hmap[nums[i]] = 1
		ans = []
		for (key, val) in hmap.items():
			heappush(ans, (-val, -key))
		final = []
		for _ in range(k):
			temp = heappop(ans)
			temp = -temp[1]
			final.append(temp)
		return final


class Solution:

	def topK(self, nums, k):
		d = {}
		for i in range(len(nums)):
			if nums[i] in d:
				d[nums[i]] += 1
			else:
				d[nums[i]] = 1
		ans = [0] * len(d)
		j = 0
		for i in d:
			ans[j] = [d[i], i]
			j += 1
		ans.sort(reverse=True)
		res = [0] * k
		for i in range(k):
			res[i] = ans[i][1]
		return res


class Solution:

	def topK(self, nums, k):
		from collections import defaultdict
		hash = defaultdict(int)
		for e in nums:
			hash[e] += 1
		ans = []
		for (i, v) in hash.items():
			ans.append([i, v])
		ans.sort(reverse=True, key=lambda x: (x[1], x[0]))
		res = []
		for i in range(k):
			res.append(ans[i][0])
		return res


class Solution:

	def topK(self, nums, k):
		freq = {}
		for i in nums:
			freq[i] = freq.get(i, 0) + 1
		arr = [[i, freq[i]] for i in freq]
		arr.sort(key=lambda x: (-x[1], -x[0]))
		res = [arr[i][0] for i in range(min(k, len(arr)))]
		return res


class Solution:

	def topK(self, nums, k):
		dict1 = {}
		for i in nums:
			dict1[i] = 1 + dict1.get(i, 0)
		keys = list(dict1.keys())
		values = list(dict1.values())
		lst = []
		for i in range(len(keys)):
			lst.append([values[i], keys[i]])
		lst = sorted(lst, reverse=True)
		ans = []
		for i in range(k):
			ans.append(lst[i][1])
		return ans


import heapq
from collections import Counter

class Solution:

	def topK(self, nums, k):
		count = Counter(nums)
		heap = [(-c, -i) for (i, c) in count.items()]
		heapq.heapify(heap)
		top = []
		for _ in range(k):
			(c, i) = heapq.heappop(heap)
			top.append(-i)
		return top


class Solution:

	def topK(self, n, k):
		d = {}
		for i in n:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
		l = []
		for i in d:
			l.append([i, d[i]])
		l.sort(key=lambda x: (-x[1], -x[0]))
		ans = []
		for i in range(k):
			ans.append(l[i][0])
		return ans


class Solution:

	def topK(self, nums, k):
		nums.sort(reverse=True)
		l = []
		c = 1
		for i in range(0, len(nums) - 1):
			if nums[i] == nums[i + 1]:
				c += 1
			else:
				l.append([c, nums[i]])
				c = 1
		if nums[n - 1] == nums[i]:
			l.append([c, nums[i]])
		else:
			l.append([1, nums[-1]])
		l.sort(reverse=True)
		res = []
		for i in range(k):
			res.append(l[i][1])
		return res