712059. Maximum Frequency

Medium

Maximum Frequency

Summary

Given an array of integers, find the maximum frequency of a number in the array. The frequency of a number is the count of its occurrences in the array.

Detailed Explanation

The problem requires us to write an algorithm that finds the maximum frequency of a number in the given array. We can solve this problem by creating a hashmap (or dictionary) to store the frequency of each number and then find the maximum value in the hashmap.

Here is the step-by-step breakdown:

  1. Create an empty hashmap (or dictionary).
  2. Iterate through the array.
  3. For each element, increment its count in the hashmap.
  4. After iterating through the entire array, find the maximum value in the hashmap, which will be the maximum frequency of a number in the array.

Here is an ASCII art diagram to illustrate the steps:

          +---------------+
          |  Create HashMap  |
          +---------------+
                  |
                  |  Iterate through array
                  v
          +---------------+
          |  Increment count  |
          +---------------+
                  |
                  |  Find maximum frequency
                  v
          +---------------+
          |  Return result    |
          +---------------+

The time complexity of this solution is O(n), where n is the size of the array, as we need to iterate through the entire array once. The space complexity is also O(n) as in the worst case, every element in the array could be unique and thus stored in the hashmap.

Optimized Solutions

Java

View on GeeksforGeeks
import java.util.HashMap;
import java.util.Map;

public class Main {
    public static int maxFrequency(int[] arr) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int num : arr) {
            map.put(num, map.getOrDefault(num, 0) + 1);
        }
        return map.values().stream().max(Integer::compareTo).get();
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6};
        System.out.println(maxFrequency(arr)); // Output: 1
    }
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.