Maintain hashmap with count of each element, and keep on adding elements to min heap, till heap has less than k elements. When heap is of size k, if next element count is greater than count of root element of min heap, remove root of min heap and add this new element. When all elements has been processed, heap will contain k most frequent elements