← 返回 bytedance 的题目列表Top K Frequent Elements
类型:online_judge
Given a non-empty integer array, return the k most frequent elements. You may assume that the time complexity is better than O(n log n).
Input
An integer array nums of length n, 1 <= n <= 10^5, with elements in the range [-10^4, 10^4].
An integer k, 1 <= k <= n.
Output
An integer array of length k, containing the k most frequent elements in any order.
Example
Input: nums = [1,1,1,2,2,3], k = 2
Output: [1,2]
Input: nums = [1], k = 1
Output: [1]
Example
Input
1 1 1 2 2 3
2