Top K Frequent Elements

MediumHeap
Next
HeapHash MapBucket Sort

Given an array, find the k values that appear most often. Print them from most frequent to least frequent. If two values have the same frequency, print the smaller value first.

Input

Line 1: n and k. Line 2: n integers.

Output

Print the k most frequent values on one line, in the order described above.

Constraints

1 <= k <= number of distinct values <= n <= 10^5 -10^4 <= each value <= 10^4

Example 1
Input
6 2
1 1 1 2 2 3
Output
1 2

1 appears 3 times and 2 appears twice.

Hints

Top K Frequent Elements - Medium Heap Problem | Skill Shiksha