Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order.
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.
First question: how often does each value appear? You have to scan nums and tally.