Given a non-empty array of integers, return the k most frequent elements. Use a hash map and a heap for efficient lookup and retrieval.
Given a non-empty array of integers, return the k most frequent elements. Use a hash map and a heap for efficient lookup and retrieval. The returned array should be ordered by appearance frequency first, and if there is a tie in frequency, the element with the smallest index in the original array will appear first.