Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order.
It is guaranteed that the answer is unique.
Example 1:
Input: nums = [1, 1, 1, 2, 2, 3], k = 2
Output: [1, 2]
Explanation: The element 1 appears 3 times and 2 appears 2 times. These are the two most frequent elements.
Example 2:
Input: nums = [1], k = 1
Output: [1]
Explanation: There is only one element, so it is the most frequent.
Example 3:
Input: nums = [4, 4, 4, 2, 2, 3, 3, 3, 1], k = 2
Output: [4, 3]
Explanation: 4 and 3 both appear 3 times, which is the highest frequency. 2 appears only 2 times.
1 <= nums.length <= 10⁵-10⁴ <= nums[i] <= 10⁴k is in the range [1, the number of unique elements in the array]