Skip to content

Commit 01566bc

Browse files
committed
day_1 continuation
1 parent 61ce45c commit 01566bc

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

leetcode-master/arrays_and_hashing/top_k_elements.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
44
Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order.
55
6-
7-
86
Example 1:
97
108
Input: nums = [1,1,1,2,2,3], k = 2
@@ -27,7 +25,18 @@
2725
"""
2826

2927
from typing import List
30-
28+
from collections import Counter
29+
30+
#solution by rubix-coder
31+
class SolutiontopKFrequent:
32+
def topKFrequent(nums: List[int], k: int) -> List[int]:
33+
most_frequent = Counter(nums)
34+
return list(most_frequent.keys())[:k]
35+
36+
nums = [1,1,1,2,2,3]
37+
k = 2
38+
result = SolutiontopKFrequent.topKFrequent(nums=nums,k=k)
39+
print(result)
3140

3241
class Solution:
3342
def topKFrequent(self, nums: List[int], k: int) -> List[int]:

0 commit comments

Comments
 (0)