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

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

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,2,3,1]
<strong>Output:</strong> 2
<strong>Explanation:</strong> 
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.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,2,3,1,4,2]
<strong>Output:</strong> 6
<strong>Explanation:</strong> 
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.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>nums.length</code> will be between 1 and 50,000.</li>
	<li><code>nums[i]</code> will be an integer between 0 and 49,999.</li>
</ul>

class Solution {
public:
    int findShortestSubArray(vector<int>& nums) {
        int maxNumCount = 0;
        unordered_map<int, int> numsCount;
        unordered_map<int, pair<int, int>> firstAndLastOccurance;
        unordered_set<int> possibleInts;
        for(int i = 0; i < nums.size(); i++){
            if(numsCount.find(nums[i]) == numsCount.end()){
                numsCount[nums[i]] = 1;
            }else{
                numsCount[nums[i]]++;
            }

            if(numsCount[nums[i]] == maxNumCount){
                possibleInts.insert(nums[i]);
            }

            if(numsCount[nums[i]] > maxNumCount){
                maxNumCount = numsCount[nums[i]];
                possibleInts.clear();
                possibleInts.insert(nums[i]);
            }


            if(firstAndLastOccurance.find(nums[i]) == firstAndLastOccurance.end()){
                firstAndLastOccurance[nums[i]].first = i;
                firstAndLastOccurance[nums[i]].second = i;
            }

            firstAndLastOccurance[nums[i]].second = i;
        }

        int minSubArray = -1;
        for(int num : possibleInts){
            int subArraySize = (firstAndLastOccurance[num].second - firstAndLastOccurance[num].first) + 1;

            if(minSubArray == -1 || subArraySize < minSubArray){
                minSubArray = subArraySize;
            }
        }

        return minSubArray;
    }




        // int max = 0;
        // for(auto& [key, val] : numsCount){
        //     if(val > max){
        //         max = val;
        //     }
        // }


    //     vector<int> subArraySizes;
    //     for(int num : possibleInts){
    //         for(int i = 0; i < nums.size(); i++){
    //             if(nums[i] == num){
    //                 int subArraySize = 1;
    //                 int j = i + 1;
    //                 int numCount = 1;
    //                 while(numCount < max){
    //                     if(nums[j] == num){
    //                         numCount++;
    //                     }
    //                     subArraySize++;
    //                     j++;
    //                 }
    //                 subArraySizes.push_back(subArraySize);
    //                 break;
    //             }
    //         }
    //     }

    //     int minSubArraySize = -1;
    //     for(int size : subArraySizes){
    //         if(minSubArraySize == -1 || size < minSubArraySize){
    //             minSubArraySize = size;
    //         }
    //     }
    //     return minSubArraySize;
    // }
};
32692