Given an integer array, return the k elements that appear most frequently. To keep the output deterministic, sort by frequency descending and then by element value ascending when frequencies tie.
nums = integer array, k = number of frequent elements to return
k most frequent values ordered by frequency descending and value ascending on ties
Example 1:
Input:
nums = [1,1,1,2,2,3] k = 2
Output:
[1,2]
Explanation:
1 appears 3 times and 2 appears 2 times.
Example 2:
Input:
nums = [4,1,-1,2,-1,2,3] k = 2
Output:
[-1,2]
Explanation:
-1 and 2 both appear twice, and -1 comes first on the tie.