Skip to the code
cf.
+25 XP

#384 Trending Hashtags

medium

A social app's sidebar shows the k hashtags used most in the last hour. You get every use as a string in tags.

Write trending(tags, k) that returns the k most used hashtags, most used first. When two hashtags are used the same number of times, the one that comes first in normal string order (<) goes first. k is between 1 and the number of different hashtags.

Example 1

Input: tags = ["css","js","css","html","js","css"], k = 2
Output: ["css","js"]

css is used 3 times and js twice.

Example 2

Input: tags = ["vue","react","svelte","react","vue"], k = 2
Output: ["react","vue"]

react and vue are tied at 2; react comes first alphabetically.

Constraints

  • 1 <= tags.length <= 10^5
  • 1 <= k <= number of different tags
  • Tags are non-empty lowercase strings.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).