Skip to the code
cf.
+25 XP

#208 Most Used Reactions

medium

A chat app shows the most popular emoji reactions in a thread. reactions lists every reaction used, as short names like "thumbs-up".

Write topReactions(reactions, k) that returns the k most used names. Order them by count, highest first; names with equal counts go in dictionary order (by character codes). Aim for O(n) work beyond the final tie ordering.

Example 1

Input: reactions = ["heart","fire","heart","smile","fire","heart"], k = 2
Output: ["heart","fire"]

heart was used 3 times and fire twice.

Example 2

Input: reactions = ["b","a","c","a","b"], k = 2
Output: ["a","b"]

a and b are tied at 2, so dictionary order decides.

Constraints

  • 1 <= reactions.length <= 100000
  • 1 <= k <= number of distinct names
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).