':type nums: List[int]
:type k: int
:rtype: List[int]'

def topKFrequent(self, nums, k):

    counts = collections.defaultdict(int)
    for i in nums:
        counts[i] += 1
    p = []
    for (key, val) in counts.iteritems():
        p.append((val, key))
    self.kthElement(p, k)
    result = []
    for i in xrange(k):
        result.append(p[i][1])
    return result
