Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Same approach but two different ways of writing sam
- # Solution 1
- class Solution:
- def topKFrequent(self, nums: List[int], k: int) -> List[int]:
- from collections import defaultdict
- hashmap = defaultdict(int)
- for num in nums:
- hashmap[num] += 1
- keys, values = list(hashmap.keys()), list(hashmap.values())
- values, keys = zip(*sorted(zip(values, keys), reverse=True))
- return keys[:k]
- # Solution 2
- class Solution:
- def topKFrequent(self, nums: List[int], k: int) -> List[int]:
- from collections import Counter
- hashmap = Counter(nums)
- freqs = [[key, value] for key, value in hashmap.items()]
- freqs.sort(key = lambda x : x[1], reverse = True)
- return [x[0] for x in freqs[:k]]
Advertisement
Add Comment
Please, Sign In to add comment