find the kth least common element in an array in python

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.py
import heapq

def kth_least_common_element(arr, k):
    # sort the array in ascending order
    arr.sort()
    
    # create a heap to keep track of the frequency of each element in the array
    freq_heap = []
    heapq.heapify(freq_heap)
    
    # create a set of unique elements in the array
    unique_set = set(arr)
    
    # iterate through the elements in the array, updating the frequency count in the heap
    for num in unique_set:
        freq = arr.count(num)
        heapq.heappush(freq_heap, (freq, num))
    
    # 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
    freq_list = []
    while freq_heap:
        freq, num = heapq.heappop(freq_heap)
        freq_list.append((freq, num))
    
    # sort the list of tuples in ascending order based on the frequency count
    freq_list.sort()
    
    # return the kth least common element
    return freq_list[k-1][1]
987 chars
30 lines

Example usage:

main.py
>>> arr = [4, 2, 1, 4, 4, 3, 1, 2, 2]
>>> kth_least_common_element(arr, 2)
2
77 chars
4 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.

related categories

gistlibby LogSnag