One way to find the kth least common element in an array is to use a combination of sorting, heap, and set data structures. In this approach, we first sort the array in ascending order, and then use a heap to keep track of the frequency of each element in the array. We then create a set of unique elements in the array.
Next, we iterate through the elements in the array, updating the frequency count in the heap as we go along. Once we have iterated through all the elements, we use the heap to create a list of tuples where the first element of the tuple is the frequency count, and the second element is the value of the element.
Finally, we sort this list of tuples in ascending order based on the frequency count, and return the kth element from the sorted list.
Here's the implementation:
main.py987 chars30 lines
Example usage:
main.py77 chars4 lines
In the example above, the 2nd least common element in the array [4, 2, 1, 4, 4, 3, 1, 2, 2]
is 2
, and the function returns 2
.
gistlibby LogSnag