K Most Frequent — Solution & Editorial
Count with a hash map, sort the (value, count) pairs by (−count, value), take K.
Complexity: O(N + D log D) for D distinct values
Count with a hash map, sort the (value, count) pairs by (−count, value), take K.
Complexity: O(N + D log D) for D distinct values