Skip to content

Instantly share code, notes, and snippets.

@igorLisovitskiy
Created November 2, 2021 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igorLisovitskiy/3892fb3eed763cdd605010661452aee9 to your computer and use it in GitHub Desktop.
Save igorLisovitskiy/3892fb3eed763cdd605010661452aee9 to your computer and use it in GitHub Desktop.
class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
output = []
counts = {}
for i in nums:
counts[i] = counts.get(i, 0) + 1
for top in range(k):
top_key = None
top_value = 0
for key, value in counts.items():
if value > top_value:
top_value = value
top_key = key
output.append(top_key)
counts.pop(top_key)
return output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment