aboutsummaryrefslogtreecommitdiff
path: root/count_element_max_freq.py
diff options
context:
space:
mode:
authorjack <[email protected]>2024-06-14 13:13:15 -0600
committerjack <[email protected]>2024-06-14 13:13:15 -0600
commitd343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch)
tree96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /count_element_max_freq.py
downloadleetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz
leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip
initial
Diffstat (limited to 'count_element_max_freq.py')
-rw-r--r--count_element_max_freq.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/count_element_max_freq.py b/count_element_max_freq.py
new file mode 100644
index 0000000..5c8e6b2
--- /dev/null
+++ b/count_element_max_freq.py
@@ -0,0 +1,33 @@
+# 3005. Count Elements With Maximum Frequency
+
+"""
+given an array 'nums' consisting of positive integers. return the total
+frequencies of elements in 'nums' such that those elements all have the
+maximum frequency. the frequency of an element is the number of occurences of
+that in the array
+"""
+
+
+class Solution(object):
+ def maxFrequencyElements(self, nums):
+ """
+ :type nums: List[int]
+ :rtype: int
+ """
+ u_map = {}
+ for i in nums:
+ u_map[i] = u_map.get(i, 0) + 1
+ max_i = 0
+ for key, val in u_map.items():
+ max_i = max(max_i, val)
+ ans = 0
+ for key, val in u_map.items():
+ if val == max_i:
+ ans += val
+ return ans
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.maxFrequencyElements([1, 2, 2, 3, 1, 4]))
+ print(obj.maxFrequencyElements([1, 2, 3, 4, 5]))