Smallest subarray with all occurrences of a most frequent element

Given an array, A.Let x be an element in the array.x has the maximum frequency in the array.Find the smallest subsegment of the array which also has x as the maximum frequency element.

Examples:

Input :  arr[] = {4, 1, 1, 2, 2, 1, 3, 3} 
Output :   1, 1, 2, 2, 1
The most frequent element is 1. The smallest
subarray that has all occurrences of it is
1 1 2 2 1

Input :  A[] = {1, 2, 2, 3, 1}
Output : 2, 2
Note that there are two elements that appear
two times, 1 and 2. The smallest window for
1 is whole array and smallest window for 2 is
{2, 2}. Since window for 2 is smaller, this is
our output.
class Solution {
    public int[] smallestSubsegment(int[] nums) {
        // Frequency map
        HashMap<Integer, Integer> map = new HashMap<>();
        int maxFreq = 0;
        for (int x : nums) {
            map.put(x, map.getOrDefault(x, 0) + 1);
            maxFreq = Math.max(maxFreq, map.get(x));
        }
        // Map of elements with max freq -> [lower limit,upper limit]
        Map<Integer, int[]> maxFreqMap = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int x = nums[i];
            if (map.get(x) == maxFreq) {
                maxFreqMap.computeIfAbsent(x, k -> new int[] { Integer.MAX_VALUE, Integer.MIN_VALUE });
                maxFreqMap.get(x)[0] = Math.min(maxFreqMap.get(x)[0], i);
                maxFreqMap.get(x)[1] = Math.max(maxFreqMap.get(x)[1], i);
            }
        }
        // Finding the smallest span, which contains maxFreq element
        int start = -1, end = -1;
        for (int x : maxFreqMap.keySet()) {
            if ((start == -1 && end == -1) || (end - start) > (maxFreqMap.get(x)[1] - maxFreqMap.get(x)[0])) {
                start = maxFreqMap.get(x)[0];
                end = maxFreqMap.get(x)[1];
            }
        }
        int[] ans = new int[end - start + 1];
        for (int i = 0; i < end - start + 1; i++) {
            ans[i] = nums[i + start];
        }
        return ans;
    }
}

Last updated