How to optimize the algorithm to only push K elements to heap?

Once we build the heap, we want to remove to remove duplicates from the elements that have fewer duplicates than the elements that have greater number of duplicates (in a greedy manner). If we are to insert only K elements into the heap, how can we be sure that we have gotten all/as many as possible of the elements with fewer duplicates? It could be that the first K elements we insert in the heap will be the ones with the greatest number of duplicates.

Type your question above this line.