-
Notifications
You must be signed in to change notification settings - Fork 0
/
count-elements-with-maximum-frequency_3005.py
42 lines (29 loc) · 1.23 KB
/
count-elements-with-maximum-frequency_3005.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# You are 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 occurrences of that element in the array.
# Example 1:
# Input: nums = [1,2,2,3,1,4]
# Output: 4
# Explanation: The elements 1 and 2 have a frequency of 2 which is the maximum frequency in the array.
# So the number of elements in the array with maximum frequency is 4.
# Example 2:
# Input: nums = [1,2,3,4,5]
# Output: 5
# Explanation: All elements of the array have a frequency of 1 which is the maximum.
# So the number of elements in the array with maximum frequency is 5.
# Constraints:
# 1 <= nums.length <= 100
# 1 <= nums[i] <= 100
# ---------------------------------------Runtime 43 ms Beats 60.41% Memory 16.62 MB Beats 70.06%---------------------------------------
from collections import Counter
from typing import List
class Solution:
def maxFrequencyElements(self, nums: List[int]) -> int:
c = Counter(nums)
m: int = c[max(c, key=c.get)]
res: int = len(nums)
if m != 1:
res = 0
for num in nums:
res += c[num] == m
return res