Top K Frequent Elements
Given an integer array nums and an integer k, return the k most frequent elements within the array.
The test cases are generated such that the answer is always unique.
You may return the output in any order.
Example 1
- Input: nums = [1,2,2,3,3,3], k = 2
- [2,3]
- Ввод: nums = [7,7], k = 1
- output: [7]
Output: [1,2]
< /code>
my code: < /h2>
class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
number_map={}
result=[]
for num in nums:
if num in number_map:
number_map[num] +=1
else:
number_map[num] = 1
for num in number_map:
if number_map[num] >= k:
result.append(num)
return result
< /code>
Я хочу знать, почему он не работает для этого тестового примера: < /p>
nums=[1,2] k=2
Подробнее здесь: https://stackoverflow.com/questions/796 ... the-prolem