One way to find the kth most frequent element in an array is to use a hash map to count the frequencies of each element, sort the hash map by value in descending order, and return the kth key. Another approach is to use a min heap to keep track of the k most frequent elements, where the root of the heap is the minimum frequency seen so far.
Here's the implementation of the first approach:
main.rs697 chars26 lines
And here's the implementation of the second approach using a min heap:
main.rs1204 chars48 lines
gistlibby LogSnag