### 697. Degree of an Array

**Difficulty**: Easy  
**Topics**: Arrays, Hashmaps  
**Companies**: (relevant companies that asked this problem)

---

#### Problem Statement:

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`.

---

#### 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.  
  Among 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` appears 3 times.  
  The shortest subarray that has the same degree is `[2, 2, 3, 1, 4, 2]`, so the output is `6`.

---

#### Constraints:

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

---

#### Approach:

To solve this problem efficiently:
1. Track the frequency of each element to determine the degree of the array.
2. Use a hashmap to store the first and last occurrence of each element to calculate the length of subarrays.
3. Identify the elements that contribute to the degree and find the smallest subarray length that has the same frequency.

Q- Can you explain the problem using your own words? 

We have a definition,  the degree of an array represents the maxi number of frequency of the 
Element in the array 

For example 
`nums = [1, 2, 2, 3, 1]`
The degree of this array is equal to 2 ( 2 is repeated two times, 1  is repeated two times )  

 Our task is to find the smallest sub-continuous array where that has the same degree 

For example [1,2,2] is of the same degree but not the smallest possible array
[2,2] is the smallest one so we return two 

Q- what is the input/output? 

Input : List[int] 
Output: int 

Q-  can you through an example ? 


[1,2,2,3,1] 
 
Q- give me all the possible subarray of this is input array 
Sure! Let's go through the example with `nums = [1, 2, 2, 3, 1]`. Here are **all possible contiguous subarrays**:

1. `[1]`
2. `[1, 2]`
3. `[1, 2, 2]`
4. `[1, 2, 2, 3]`
5. `[1, 2, 2, 3, 1]`
6. `[2]`
7. `[2, 2]`
8. `[2, 2, 3]`
9. `[2, 2, 3, 1]`
10. `[2, 3]`
11. `[2, 3, 1]`
12. `[3]`
13. `[3, 1]`
14. `[1]`

---

Now, you want to find the **smallest** contiguous subarray that has the same degree as the original array:

1. The degree of the array is `2`, because the elements `1` and `2` both appear twice.
2. We look for subarrays that include either `1` or `2` with the same frequency.

- The shortest subarray that gives a degree of `2` is `[2, 2]` (since the element `2` appears twice in this subarray).
- Therefore, the smallest subarray length is `2`.

Thus, the output would be `2`.


Q- First we need to find the degree of the original input array right? 

hashmap={1:2,2:2,3:1} 

The degree is the max value of the hashmap → 2 

Q- the next part is to go through the search process of the smallest possible subarray. 

Using two pointers i and j 

I : responsible of specifying the left bound 
J : responsible of specifying the right bound oky 


For each possible array : 
Compute the hashmap of the current subarray 
Get the rank as the maximum frequency 
Get the length of the subarray 
If rank of the current subarray == rank the input array 
		Update the input based on the min length of it 

Q- develop for me a good pesuodCode for this ?