Python - Extremely easy to understand - 62MS - Only a dictionary


  • 0
    K

    The idea is simple.

    1. Build a dictionary of items and their frequencies.
    2. Create a list of sorted tuples of items by value from the dictionary.
    3. Return a list of k length from the sorted list.
    dictionary = {}
            for i in nums:
                if i in dictionary:
                    dictionary[i] += 1
                else:
                    dictionary[i] = 1
                    
            sorted_x = sorted(dictionary.items(), key=operator.itemgetter(1), reverse=True)
            return [x[0] for x in sorted_x][:k]
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.