This question evaluates a candidate's ability to implement efficient frequency counting and selection logic, demonstrating understanding of data structures, time and space complexity, and handling of large input sizes.
You are given an array of integers nums and an integer k.
Return the k elements that occur most frequently in nums. You may return the answer in any order.
nums
: list of integers (can include negatives)
k
: integer where
1 <= k <= number of distinct values in nums
k
integers corresponding to the most frequent values.
nums = [1,1,1,2,2,3]
,
k = 2
[1,2]
1 <= len(nums) <= 10^5