The problem. Given an integer array nums and an integer k, return the k most frequent values. The answer may be in any order, and it is guaranteed to be unique.
nums = [1, 1, 1, 2, 2, 3], k = 2 -> [1, 2]
nums = [1], k = 1 -> [1]
nums = [4, 1, -1, 2, -1, 2, 3], k = 2 -> [-1, 2]Every approach starts the same way: one pass with a hash map to count each value. The question is how to pick the k largest counts.
Free account
Sign up to read the rest of this lesson: 7 more sections, 3 drawings, a dry-run simulator and code in JavaScript, Python, Java and C++.
Still to come