2dbi
⌘K
Home/Amazon/Top K Frequent Elements
AAmazon·DSASDE-2Onsite – Coding 1

Top K Frequent Elements

Problem

Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order.

Example

nums = [1,1,1,2,2,3], k = 2
Output: [1, 2]

Constraints

  • 1 ≤ nums.length ≤ 10^5
  • k is in the range [1, number of unique elements]
  • The answer is guaranteed to be unique

Expected complexity

Better than O(n log n). Can you solve it in O(n) using bucket sort?

asked 5 days ago

Follow-up questions (0)

Add a follow-up question they asked
No follow-ups yet. Be the first to add one.