3242. Count Elements With Maximum Frequency¶
Difficulty: Easy
LeetCode Problem View on GitHub
3242. Count Elements With Maximum Frequency
Easy
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 <= 1001 <= nums[i] <= 100
Solution¶
class Solution {
public int maxFrequencyElements(int[] nums) {
int n = nums.length;
int freq[] = new int[101];
int maxi = 0, count = 0;
for (int ele : nums) {
freq[ele]++;
if (freq[ele] > maxi) {
maxi = freq[ele];
count = 1;
}
else if (freq[ele] == maxi) {
count++;
}
}
return count * maxi;
}
}
Complexity Analysis¶
- Time Complexity:
O(?) - Space Complexity:
O(?)
Approach¶
Detailed explanation of the approach will be added here