Return the k most frequent elements of the array.
Example 1
Input: nums = [1,2,1,2,1,2,3], k = 2
Output: [1,2]
Explanation: 1 and 2 have the maximum and equal frequencies; both are included in the answer.
Output: [1,2]
Explanation: 1 and 2 have the maximum and equal frequencies; both are included in the answer.
Example 2
Input: nums = [1], k = 1
Output: [1]
Explanation: The array has only one element, which is the top-1 by frequency.
Output: [1]
Explanation: The array has only one element, which is the top-1 by frequency.
Example 3
Input: nums = [1,1,1,2,2,3], k = 2
Output: [1,2]
Explanation: Elements 1 and 2 appear more often than the others.
Output: [1,2]
Explanation: Elements 1 and 2 appear more often than the others.