vector<int> topKFrequent(vector<int>& nums, int k) {
unordered_map<int, int> count;
for (int n : nums) count[n]++;
vector<pair<int, int>> entries(count.begin(), count.end());
sort(entries.begin(), entries.end(), [](auto& a, auto& b) { return a.second > b.second; });
vector<int> result;
for (int i = 0; i < k; i++) result.push_back(entries[i].first);
return result;
}
public int[] topKFrequent(int[] nums, int k) {
Map<Integer, Integer> count = new HashMap<>();
for (int n : nums) count.merge(n, 1, Integer::sum);
List<Map.Entry<Integer, Integer>> entries = new ArrayList<>(count.entrySet());
entries.sort((a, b) -> b.getValue() - a.getValue());
int[] result = new int[k];
for (int i = 0; i < k; i++) result[i] = entries.get(i).getKey();
return result;
}
def top_k_frequent(nums, k):
count = {}
for n in nums: count[n] = count.get(n, 0) + 1
entries = list(count.items())
entries.sort(key=lambda e: -e[1])
return [n for n, _ in entries[:k]]
function topKFrequent(nums: number[], k: number): number[] {
const count = new Map<number, number>();
for (const n of nums) count.set(n, (count.get(n) ?? 0) + 1);
const entries = [...count.entries()];
entries.sort((a, b) => b[1] - a[1]);
return entries.slice(0, k).map(([n]) => n);
}
vector<int> topKFrequent(vector<int>& nums, int k) {
unordered_map<int, int> count;
for (int n : nums) count[n]++;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> heap;
for (auto& [n, c] : count) {
heap.push({c, n});
if ((int)heap.size() > k) heap.pop();
}
vector<int> result;
while (!heap.empty()) { result.push_back(heap.top().second); heap.pop(); }
return result;
}
public int[] topKFrequent(int[] nums, int k) {
Map<Integer, Integer> count = new HashMap<>();
for (int n : nums) count.merge(n, 1, Integer::sum);
PriorityQueue<int[]> heap = new PriorityQueue<>((a, b) -> a[1] - b[1]);
for (Map.Entry<Integer, Integer> e : count.entrySet()) {
heap.add(new int[]{e.getKey(), e.getValue()});
if (heap.size() > k) heap.poll();
}
int[] result = new int[heap.size()];
for (int i = 0; i < result.length; i++) result[i] = heap.poll()[0];
return result;
}
def top_k_frequent(nums, k):
count = {}
for n in nums: count[n] = count.get(n, 0) + 1
heap = []
for n, c in count.items():
heapq.heappush(heap, (c, n))
if len(heap) > k: heapq.heappop(heap)
return [n for _, n in heap]
function topKFrequent(nums: number[], k: number): number[] {
const count = new Map<number, number>();
for (const n of nums) count.set(n, (count.get(n) ?? 0) + 1);
const heap = new MinHeap<[number, number]>((a, b) => a[1] < b[1]);
for (const [n, c] of count) {
heap.push([n, c]);
if (heap.size > k) heap.pop();
}
return heap.toArray().map(([n]) => n);
}
class MinHeap<T> {
private a: T[] = [];
constructor(private less: (x: T, y: T) => boolean) {}
get size() { return this.a.length; }
toArray() { return [...this.a]; }
push(v: T) {
const a = this.a;
a.push(v);
let i = a.length - 1;
while (i > 0) {
const p = (i - 1) >> 1;
if (!this.less(a[i], a[p])) break;
[a[i], a[p]] = [a[p], a[i]];
i = p;
}
}
pop(): T {
const a = this.a;
const top = a[0];
const last = a.pop()!;
if (a.length) {
a[0] = last;
let i = 0;
for (;;) {
let m = i;
const l = 2 * i + 1, r = l + 1;
if (l < a.length && this.less(a[l], a[m])) m = l;
if (r < a.length && this.less(a[r], a[m])) m = r;
if (m === i) break;
[a[i], a[m]] = [a[m], a[i]];
i = m;
}
}
return top;
}
}
vector<int> topKFrequent(vector<int>& nums, int k) {
unordered_map<int, int> count;
for (int n : nums) count[n]++;
vector<vector<int>> buckets(nums.size() + 1);
for (auto& [n, c] : count) buckets[c].push_back(n);
vector<int> result;
for (int c = (int)buckets.size() - 1; c > 0 && (int)result.size() < k; c--) {
result.insert(result.end(), buckets[c].begin(), buckets[c].end());
}
if ((int)result.size() > k) result.resize(k);
return result;
}
public int[] topKFrequent(int[] nums, int k) {
Map<Integer, Integer> count = new HashMap<>();
for (int n : nums) count.merge(n, 1, Integer::sum);
List<List<Integer>> buckets = new ArrayList<>(); for (int i = 0; i <= nums.length; i++) buckets.add(new ArrayList<>());
for (Map.Entry<Integer, Integer> e : count.entrySet()) buckets.get(e.getValue()).add(e.getKey());
List<Integer> result = new ArrayList<>();
for (int c = buckets.size() - 1; c > 0 && result.size() < k; c--) {
result.addAll(buckets.get(c));
}
int[] out = new int[k];
for (int i = 0; i < k; i++) out[i] = result.get(i);
return out;
}
def top_k_frequent(nums, k):
count = {}
for n in nums: count[n] = count.get(n, 0) + 1
buckets = [[] for _ in range(len(nums) + 1)]
for n, c in count.items(): buckets[c].append(n)
result = []
c = len(buckets) - 1
while c > 0 and len(result) < k:
result.extend(buckets[c])
c -= 1
return result[:k]
function topKFrequent(nums: number[], k: number): number[] {
const count = new Map<number, number>();
for (const n of nums) count.set(n, (count.get(n) ?? 0) + 1);
const buckets: number[][] = Array.from({ length: nums.length + 1 }, () => []);
for (const [n, c] of count) buckets[c].push(n);
const result: number[] = [];
for (let c = buckets.length - 1; c > 0 && result.length < k; c--) {
result.push(...buckets[c]);
}
return result.slice(0, k);
}
vector<int> topKFrequent(vector<int>& nums, int k) {
unordered_map<int, int> count;
for (int n : nums) count[n]++;
vector<int> uniq; for (auto& [n, c] : count) uniq.push_back(n);
auto freq = [&](int i) { return count[uniq[i]]; };
int left = 0;
int right = (int)uniq.size() - 1;
while (left < right) {
int pivotFreq = freq(right);
int store = left;
for (int i = left; i < right; i++) {
if (freq(i) > pivotFreq) {
swap(uniq[store], uniq[i]);
store++;
}
}
swap(uniq[store], uniq[right]);
if (store == k - 1) break;
if (store < k - 1) left = store + 1;
else right = store - 1;
}
return vector<int>(uniq.begin(), uniq.begin() + k);
}
public int[] topKFrequent(int[] nums, int k) {
Map<Integer, Integer> count = new HashMap<>();
for (int n : nums) count.merge(n, 1, Integer::sum);
int[] uniq = count.keySet().stream().mapToInt(Integer::intValue).toArray();
java.util.function.IntUnaryOperator freq = i -> count.get(uniq[i]);
int left = 0;
int right = uniq.length - 1;
while (left < right) {
int pivotFreq = freq.applyAsInt(right);
int store = left;
for (int i = left; i < right; i++) {
if (freq.applyAsInt(i) > pivotFreq) {
int t = uniq[store]; uniq[store] = uniq[i]; uniq[i] = t;
store++;
}
}
int t = uniq[store]; uniq[store] = uniq[right]; uniq[right] = t;
if (store == k - 1) break;
if (store < k - 1) left = store + 1;
else right = store - 1;
}
return Arrays.copyOf(uniq, k);
}
def top_k_frequent(nums, k):
count = {}
for n in nums: count[n] = count.get(n, 0) + 1
uniq = list(count.keys())
freq = lambda i: count[uniq[i]]
left = 0
right = len(uniq) - 1
while left < right:
pivot_freq = freq(right)
store = left
for i in range(left, right):
if freq(i) > pivot_freq:
uniq[store], uniq[i] = uniq[i], uniq[store]
store += 1
uniq[store], uniq[right] = uniq[right], uniq[store]
if store == k - 1: break
if store < k - 1: left = store + 1
else: right = store - 1
return uniq[:k]
function topKFrequent(nums: number[], k: number): number[] {
const count = new Map<number, number>();
for (const n of nums) count.set(n, (count.get(n) ?? 0) + 1);
const uniq = [...count.keys()];
const freq = (i: number) => count.get(uniq[i])!;
let left = 0;
let right = uniq.length - 1;
while (left < right) {
const pivotFreq = freq(right);
let store = left;
for (let i = left; i < right; i++) {
if (freq(i) > pivotFreq) {
[uniq[store], uniq[i]] = [uniq[i], uniq[store]];
store++;
}
}
[uniq[store], uniq[right]] = [uniq[right], uniq[store]];
if (store === k - 1) break;
if (store < k - 1) left = store + 1;
else right = store - 1;
}
return uniq.slice(0, k);
}