Given an integer array nums
of length n
, count the number of elements with the maximum frequency in the array.
Input: nums = [1,2,2,3,3,3]
Output: 1
Explanation: The number 3 has the maximum frequency which is 3, and there is only one number with this frequency.
def countMaxFrequencyElements(nums):
# Step 1: Count the frequency of each element
freq_count = {}
for num in nums:
if num in freq_count:
freq_count[num] += 1
else:
freq_count[num] = 1
# Step 2: Determine the maximum frequency
max_freq = max(freq_count.values())
# Step 3: Count how many elements have the maximum frequency
max_freq_count = 0
for count in freq_count.values():
if count == max_freq:
max_freq_count += 1
return max_freq_count
# Test example from the problem statement
print(countMaxFrequencyElements([1, 2, 2, 3, 3, 3])) # Output should be 1
nums
, which takes (O(n)) time.Thus, the overall time complexity is (O(n)).
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?