← Practice · Hash map

Top K Frequent Elements Medium

Given an integer array nums and an integer k, return the k most frequent elements in any order. The answer is guaranteed to be unique.

Examples

  • nums = [1,1,1,2,2,3], k = 2 -> [1,2]
  • nums = [1], k = 1 -> [1]

Define a function named topKFrequent in JavaScript.

00:00

JavaScript · Tab indents · Ctrl/⌘+Enter runs the tests