Given a non-empty array of non-negative integers nums, the degree of this array is defined as the maximum frequency of any one of its elements.

Your task is to find the smallest possible length of a (contiguous) subarray of nums, that has the same degree as nums.

&nbsp;
Example 1:

Input: nums = [1,2,2,3,1]
Output: 2
Explanation: 
The input array has a degree of 2 because both elements 1 and 2 appear twice.
Of the subarrays that have the same degree:
[1, 2, 2, 3, 1], [1, 2, 2, 3], [2, 2, 3, 1], [1, 2, 2], [2, 2, 3], [2, 2]
The shortest length is 2. So return 2.


Example 2:

Input: nums = [1,2,2,3,1,4,2]
Output: 6
Explanation: 
The degree is 3 because the element 2 is repeated 3 times.
So [2,2,3,1,4,2] is the shortest subarray, therefore returning 6.


&nbsp;
Constraints:


	nums.length will be between 1 and 50,000.
	nums[i] will be an integer between 0 and 49,999.


class Solution:
    def findShortestSubArray(self, nums):
        # Group indexes by element type
        d = defaultdict(list)
        for i,x in enumerate(nums):
            d[x].append(i)
        #
        # Find highest Degree
        m = max([ len(v) for v in d.values() ])
        #
        # Find shortest span for max. degree
        best = len(nums)
        for v in d.values():
            if len(v)==m:
                best = min(best,v[-1]-v[0]+1)
        return best
class Solution {
    public int findShortestSubArray(int[] nums) {
       int ans = Integer.MAX_VALUE;
        Map<Integer, Integer> count = new HashMap<>();
        Map<Integer, Integer> startIndex = new HashMap<>();
        Map<Integer, Integer> endIndex = new HashMap<>();

        for (int i = 0; i < nums.length; i++) {
            count.put(nums[i], count.getOrDefault(nums[i], 0) + 1);
        }

        for (int i = 0; i < nums.length; i++) {
            int no = nums[i];
            if (!startIndex.containsKey(no)) {
                startIndex.put(no, i);
            }
            endIndex.put(no, i);
        }

        int degree = Integer.MIN_VALUE;
        for (Integer key : count.keySet()) {
            degree = Math.max(degree, count.get(key));
        }

        for (Integer key : count.keySet()) {
            if (count.get(key) == degree) {
                int arraySize = endIndex.get(key) - startIndex.get(key) + 1;
                ans = Math.min(ans, arraySize);
            }
        }

        return ans; 
    }
}
class Solution {
public:
    int findShortestSubArray(vector<int>& nums)
    {
        unordered_map<int,int> cnt,first;
        int deg=0,ans=0;
        for(int i=0;i<nums.size();i++)
        {
            if(cnt[nums[i]]==0)
                first[nums[i]]=i;
            cnt[nums[i]]++;
            if(cnt[nums[i]]>deg)
            {
                ans=i-first[nums[i]]+1;
                deg=cnt[nums[i]];
            }
            else if(cnt[nums[i]]==deg)
            {
                ans=min(ans,i-first[nums[i]]+1);
            }

        }
        return ans;

    }
};
//if you like the solution plz upvote.
/**
 * 1. compute all positions for each number
 * 2. filter arrays of max length
 * 3. select minimum difference between its first and last elems
 */
var findShortestSubArray = function(nums) {
    const numPositions = {};
    for (let i=0; i<nums.length; i++) {
        const num = nums[i];
        if (numPositions[num] == null) numPositions[num] = [];
        numPositions[num].push(i);
    }

    let maxLen = 0;
    // will store the positions of most frequent numbers
    let maxPos = [];
    for (const positions of Object.values(numPositions)) {
        const curLen = positions.length;
        if (curLen === maxLen) {
            maxPos.push(positions);
        } else if (curLen > maxLen) {
            maxLen = curLen;
            maxPos = [positions]
        }
    }

    let minDist = Number.MAX_SAFE_INTEGER;
    for (const positions of maxPos) {
        minDist = Math.min(minDist, positions[positions.length-1] - positions[0] + 1);
    }
    return minDist;
};
Degree of an Array