Top K Frequent Elements Visualizer & Solution | Rulcode
Loading problem details...
137. Top K Frequent Elements
PRO
Medium
Topics
Companies
Hint
Understand Before Coding
Visualize This Problem
Open Visualizer
Draw Your Approach
Draw
Example 1:
Input:nums = [1,1,1,2,2,3], k = 2
Output:[1,2]
Explanation:The elements 1 and 2 appear most frequently. 1 appears 3 times, 2 appears 2 times, and 3 appears once. Since k=2, we return the two most frequent elements [1, 2].
Example 2:
Input:nums = [1], k = 1
Output:[1]
Explanation:The element 1 appears once. Since k=1, we return [1].
Example 3:
Input:nums = [4,1,-1,2,2,2,3], k = 2
Output:[2, 1]
Explanation:2 appears 3 times, 1 appears 1 time, -1 appears 1 time, 4 appears 1 time, 3 appears 1 time. The two most frequent elements are 2 and 1.