Sign In
You are coding as a Guest. Sign in with your RoleNest account to permanently track your streak, earn XP, and climb the Campus Leaderboard!
Sign In with RoleNest
🔥Top K Frequent Elements: Trending AnalyticsMedium
DAILY PROBLEM OF THE DAY+50 XP • Daily Streak

Solve today's challenge or tackle one of the 3 Super Hard challenges for +150 XP.

MediumArrays & Hashing•Acceptance: 62.8%

Top K Frequent Elements: Trending Analytics

Targeted in FAANG & Tech OA:GoogleAmazonMicrosoftSwiggyUber
Real-World Engineering Context
Powers real-time trending hashtags on Twitter/X, popular product counters on Amazon, and hot cache identification at Cloudflare.
Given an integer array `nums` and an integer `k`, return the `k` most frequent elements in descending order of frequency. If frequencies match, return any valid order.

Sample Test Cases

Input: [[1,1,1,2,2,3],2]
Expected: [1,2]
Input: [[1],1]
Expected: [1]

Constraints

  • 1 <= nums.length <= 10^5
  • -10^4 <= nums[i] <= 10^4
  • k is in the range [1, the number of unique elements in the array].
Recruiter Fast-Track ReferralVerified Candidate
Direct pipeline to Google, Amazon, Microsoft, Swiggy, & Uber recruiters
DevScore: 750/1000

Top DevScore profiles bypass resume screening filters. Every verified problem solve writes authentic proof-of-work to your profile and dispatches you directly into employer inboxes on RoleNest.