LC 347 — Problem

Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order.

Input: nums = 111223, k = 2
Output: 12
Input: nums = [1], k = 1
Output: [1]

Constraints: 1 ≤ nums.length ≤ 10⁵ · k is in the range [1, number of unique elements] · the answer is guaranteed to be unique.

Return the top 3 most frequent values from nums. For this input, element 1 leads — it appears 4 times, more than any other value. Watch the naive approach find it, then see what that approach costs at scale.

nums (11 values, 5 distinct)
1
0
1
1
1
2
1
3
2
4
2
5
2
6
3
7
3
8
4
9
5
10

First question: how often does each value appear? You have to scan nums and tally.