From 6c85848d57095bee7b9c387a9d3fab51ff28bc6d Mon Sep 17 00:00:00 2001 From: Sakshi Vattikuti <51375116+sakshi2912@users.noreply.github.com> Date: Mon, 4 Oct 2021 18:57:17 +0530 Subject: [PATCH 1/2] 347 Top K Frequent Elements Python implementation, accepted on leetcode. Resolves issue #513 --- Python/347_Top_K_Frequent_Elements.py | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 Python/347_Top_K_Frequent_Elements.py diff --git a/Python/347_Top_K_Frequent_Elements.py b/Python/347_Top_K_Frequent_Elements.py new file mode 100644 index 00000000..fbee8742 --- /dev/null +++ b/Python/347_Top_K_Frequent_Elements.py @@ -0,0 +1,7 @@ +class Solution: + def topKFrequent(self, nums: List[int], k: int) -> List[int]: + + freq = Counter(nums) + t = list(freq.most_common(k)) + return [x[0] for x in t] + From be1ac4aa9aa0c2a7fcb79d43089c33f6f9cc3e0f Mon Sep 17 00:00:00 2001 From: Sakshi Vattikuti <51375116+sakshi2912@users.noreply.github.com> Date: Mon, 4 Oct 2021 19:01:41 +0530 Subject: [PATCH 2/2] Update 347_Top_K_Frequent_Elements.py --- Python/347_Top_K_Frequent_Elements.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Python/347_Top_K_Frequent_Elements.py b/Python/347_Top_K_Frequent_Elements.py index fbee8742..c6b5f3b5 100644 --- a/Python/347_Top_K_Frequent_Elements.py +++ b/Python/347_Top_K_Frequent_Elements.py @@ -1,3 +1,14 @@ +''' +Using python's library: collections +# from collections import Counter + +Use the counter object to store all frequencies of the list. +Usimg the most_common method, which takes an argument N, signifying the number of most common elements to be returned. + +The returned list consists of tuples (num, freq). +Return the first element of each tuple as the final answer. + +''' class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: