1224. Maximum Equal Frequency LeetCode Solution

In this guide, you will get 1224. Maximum Equal Frequency LeetCode Solution with the best time and space complexity. The solution to Maximum Equal Frequency problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Maximum Equal Frequency solution in C++
  4. Maximum Equal Frequency solution in Java
  5. Maximum Equal Frequency solution in Python
  6. Additional Resources
1224. Maximum Equal Frequency LeetCode Solution image

Problem Statement of Maximum Equal Frequency

Given an array nums of positive integers, return the longest possible length of an array prefix of nums, such that it is possible to remove exactly one element from this prefix so that every number that has appeared in it will have the same number of occurrences.
If after removing one element there are no remaining elements, it’s still considered that every appeared number has the same number of ocurrences (0).

Example 1:

Input: nums = [2,2,1,1,5,3,3,5]
Output: 7
Explanation: For the subarray [2,2,1,1,5,3,3] of length 7, if we remove nums[4] = 5, we will get [2,2,1,1,3,3], so that each number will appear exactly twice.

Example 2:

Input: nums = [1,1,1,2,2,2,3,3,3,4,4,4,5]
Output: 13

Constraints:

2 <= nums.length <= 105
1 <= nums[i] <= 105

Complexity Analysis

  • Time Complexity:
  • Space Complexity:

1224. Maximum Equal Frequency LeetCode Solution in C++

class Solution {
 public:
  int maxEqualFreq(vector<int>& nums) {
    int ans = 0;
    int maxFreq = 0;
    unordered_map<int, int> count;
    unordered_map<int, int> freq;

    for (int i = 0; i < nums.size(); ++i) {
      const int num = nums[i];
      --freq[count[num]];
      ++count[num];
      ++freq[count[num]];
      maxFreq = max(maxFreq, count[num]);
      if (maxFreq == 1 || maxFreq * freq[maxFreq] == i ||
          (maxFreq - 1) * (freq[maxFreq - 1] + 1) == i)
        ans = i + 1;
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

1224. Maximum Equal Frequency LeetCode Solution in Java

class Solution {
  public int maxEqualFreq(int[] nums) {
    int ans = 0;
    int maxFreq = 0;
    Map<Integer, Integer> count = new HashMap<>();
    Map<Integer, Integer> freq = new HashMap<>();

    for (int i = 0; i < nums.length; ++i) {
      final int currentFreq = count.getOrDefault(nums[i], 0);
      freq.merge(currentFreq, -1, Integer::sum);
      final int updatedFreq = currentFreq + 1;
      count.put(nums[i], updatedFreq);
      freq.merge(updatedFreq, 1, Integer::sum);
      maxFreq = Math.max(maxFreq, updatedFreq);
      if (maxFreq == 1 || maxFreq * freq.get(maxFreq) == i ||
          (maxFreq - 1) * (freq.get(maxFreq - 1) + 1) == i)
        ans = i + 1;
    }

    return ans;
  }
}
// code provided by PROGIEZ

1224. Maximum Equal Frequency LeetCode Solution in Python

class Solution:
  def maxEqualFreq(self, nums: list[int]) -> int:
    ans = 0
    maxFreq = 0
    count = collections.Counter()
    freq = collections.Counter()

    for i, num in enumerate(nums):
      freq[count[num]] -= 1
      count[num] += 1
      freq[count[num]] += 1
      maxFreq = max(maxFreq, count[num])
      if maxFreq == 1 or maxFreq * freq[maxFreq] == i or (maxFreq - 1) * (
              freq[maxFreq - 1] + 1) == i:
        ans = i + 1

    return ans
# code by PROGIEZ

Additional Resources

See also  224. Basic Calculator LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.