Top K Frequent Elements

Medium · Heap & Counting · Array · Hash Table · Heap · Bucket Sort

Problem

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

Examples

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

1 appears three times, 2 twice.

Input: nums = [1], k = 1
Output: [1]

Constraints

Approaches

Follow-up

Your algorithm should run faster than O(n log n), where n is the array's length.

Related problems

Start free on Cariara · Coding problems · System design studio

Starting Python
No results yet.

          
↑↓ move ↵ open esc close

Labs

Loading labs…

Run with your own input

Starting Python

Loading Python…