2
public int findShortestSubArray(int[] nums) {3
// The int is an array of [firstEncounter, lastEncounter, frequency]4
HashMap<Integer, int[]> map = new HashMap<>();5
for (int i = 0; i < nums.length; i++) {7
// If the key does not exist in the map, we put it with the first encounter and last encounter8
// set to the current position, 'i', and the freqency 19
if (!map.containsKey(nums[i])) {10
map.put(nums[i], new int[] {i, i, 1});13
// If it does exist, we update the last encounter to the current position and we increase the16
int[] arr = map.get(nums[i]);19
map.put(nums[i], arr);23
int maxFreq = Integer.MIN_VALUE;26
int minDist = Integer.MAX_VALUE;28
// Going through all the values of the HashMap29
for (int[] value : map.values()) {30
// value[0] = the first encounter index31
// value[1] = the last encounter index32
// value[2] = frequency34
// If the frecuency is greater than the maxFreq, then we update it and also set the minDist35
if (value[2] > maxFreq) {37
minDist = value[1] - value[0] + 1;40
// If the frecuency is equal to the current max, we take the minimum between the exiting41
// minDist and the minimum distance for the current value42
else if (value[2] == maxFreq) {43
minDist = Math.min(minDist, value[1] - value[0] + 1);