Top K Frequent Elements
Try to solve the Top K Frequent Elements problem.
We'll cover the following
Statement
Given an array of integers, arr
, and an integer, k
, return the most frequent elements.
Note: You can return the answer in any order.
Constraints:
-
arr.length
-
arr[i]
- 1
k
number of unique elements in an array. - It is guaranteed that the answer is unique.
Examples
1 of 2
Understand the problem
Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:
Top K Frequent Elements
1
What is the output if the following inputs are given?
arr = [1, 1, 2, 4, 5, 5]
k = 2
A)
[1]
B)
[5]
C)
[1, 5]
D)
[1, 2]
Question 1 of 20 attempted
Figure it out!
We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.
Try it yourself
Implement your solution in the following coding playground:
Python
usercode > main.py
from heapq import heappush, heappopdef top_k_frequent(arr, k):# Replace this placeholder return statement with your codereturn []
Click "Run" to evaluate your code.
Top K Frequent Elements