3005. Count Elements With Maximum Frequency
计算出现最多次的元素之次数之总和
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.
思路:
用哈希表计算次数 最后比大小加总
Python Code:
class Solution:
def maxFrequencyElements(self, nums: List[int]) -> int:
dic = {}
for e in nums:
if e in dic:
dic[e] += 1
else:
dic[e] = 1
m = max(dic.values())
return sum([v for v in dic.values() if v == m])