Skip to the code
cf.
+25 XP

#18 Top Searched Queries

medium

A docs site logs every search query in queries. The home page shows the k most popular ones.

Write topQueries(queries, k) that returns the k queries searched most often, from most to least searched. Break ties by putting the query that comes first in normal string order (<) first, so uppercase letters come before lowercase.

Example 1

Input: queries = ["css grid","flexbox","css grid","react","flexbox","css grid"], k = 2
Output: ["css grid","flexbox"]

css grid was searched 3 times and flexbox 2 times.

Example 2

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

All three tie at 2, so they're ordered a, b, c and the first two are kept.

Constraints

  • 1 <= queries.length <= 10^5
  • 1 <= k <= the number of distinct queries
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).