Error
|
|
---|---|
@Ibrahim Nash | 6420 |
@blackshadows | 6376 |
@mb1973 | 5594 |
@Quandray | 5231 |
@akhayrutdinov | 5111 |
@saiujwal13083 | 4510 |
@sanjay05 | 3762 |
@kirtidee18 | 3673 |
@marius_valentin_dragoi | 3522 |
@sushant_a | 3459 |
@verma_ji | 3412 |
Complete Leaderboard | |
|
|
@codersgroup18 | 783 |
@Manikanta punnam | 605 |
@sriramgoparaju99 | 590 |
@rdakka | 538 |
@praveenbgp6 | 529 |
@prankursharma31 | 518 |
@yashkaril4 | 517 |
@purohitmn02 | 467 |
@sonamnigam1999 | 443 |
@sonamkumari63928 | 441 |
@shubhamstudent5 | 433 |
Complete Leaderboard |
Given a non-empty array of integers, find the top k elements which have the highest frequency in the array. If two numbers have the same frequency then the larger number should be given preference.
Example 1:
Input:
nums = {1,1,1,2,2,3},
k = 2
Output: {1, 2}
Example 2:
Input:
nums = {1,1,2,2,3,3,3,4},
k = 2
Output: {3, 2}
Explanation: Elements 1 and 2 have the
same frequency ie. 2. Therefore, in this
case, the answer includes the element 2
as 2 > 1.
User Task:
The task is to complete the function topK() that takes the array and integer k as input and returns a list of top k frequent elements.
Expected Time Complexity : O(NlogN)
Expected Auxilliary Space : O(N)
Constraints:
1 <= N <= 103
1<=A[i]<=104
We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?
Yes