Skip to content

Instantly share code, notes, and snippets.

@amraks
Created February 11, 2019 21:26
Show Gist options
  • Save amraks/d8f9ee7df9f02e4efb060bde3f65ec68 to your computer and use it in GitHub Desktop.
Save amraks/d8f9ee7df9f02e4efb060bde3f65ec68 to your computer and use it in GitHub Desktop.
Top K frequent elements
# Leetcode https://leetcode.com/problems/top-k-frequent-elements/
def topKFrequent(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: List[int]
"""
from collections import Counter
d = Counter()
d.update(nums)
return list(map(lambda x : x[0], d.most_common()[0:k]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment