Top K Frequent Elements — C# Coding Problem
Difficulty: medium | Category: hash-map
Problem Description
Given an integer array `nums` and an integer `k`, return the `k` most frequent elements. Return them **sorted in ascending order**. You may assume the answer is unique — there is always exactly one answer with a unique top-k. **Constraints:** - `1 <= nums.length <= 10⁵` - `-10⁴ <= nums[i] <= 10⁴` - `k` is in the range `[1, number of unique elements]` **Hint:** Use a hash map for counts, then sort by frequency.
Examples
Example 1
Input: nums = [1,1,1,2,2,3], k = 2
Output: [1,2]
Explanation: 1 appears 3x, 2 appears 2x.
Example 2
Input: nums = [1], k = 1
Output: [1]