Ruby 3 lines


  • 0
    L
    def top_k_frequent(nums, k)
        map = Hash.new(0)
        nums.each{|x| map[x] += 1}
        return (map.keys.sort_by!{|e| [-map[e]]})[0...k]
    end
    

Log in to reply
 

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