Top-N Frequent Log Lines
Return the n most frequent entries in a log stream. Ties are broken lexicographically (the smaller string first).
topNFrequent(["a", "b", "a", "c", "b", "a"], 2) -> ["a", "b"] topNFrequent(["x", "y", "z", "y", "x", "z"], 2) -> ["x", "y"] // all tied, lexicographic
If fewer than n distinct lines exist, return all of them in order; n = 0 returns nothing.
Hints
Hint 1
Count first. With the counts in hand there are two finishes: bucket the distinct lines by frequency and walk the buckets from the top (O(d) time, O(d) space), or keep a min-heap of size n keyed by (count, reversed lexicographic order) so memory is bounded by n.
Hint 2
Whichever finish you pick, ties inside a frequency must come out in lexicographic order — sort each bucket, or make the heap key compare strings in reverse so the lexicographically smaller line wins.