dsa · medium
Top K Frequent Elements
Return the k most frequent values in nums. The answer is unique. Return them in **any** order.
Arguments
nums— the multiset to countk— how many distinct most-frequent values to return
Example
nums = [1,1,1,2,2,3], k = 2 → [1,2].
nums = [1], k = 1 → [1].
Constraints
1 <= nums.length <= 10^5 k is in the range [1, the number of unique values in nums] The answer is unique Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.
Examples
Example 1
Input: [1,1,1,2,2,3] 2 Expected: [1,2]
Example 2
Input: [1] 1 Expected: [1]