Finding degree of subarray in an array JavaScript

The degree of an array is defined as the maximum frequency of any element in the array. Finding the shortest subarray with the same degree is a common programming problem that involves tracking element frequencies and their positions.

const arr = [1, 2, 3, 3, 5, 6, 4, 3, 8, 3];
console.log("Array:", arr);
console.log("Degree of array: 4 (element 3 appears 4 times)");
Array: [ 1, 2, 3, 3, 5, 6, 4, 3, 8, 3 ]
Degree of array: 4 (element 3 appears 4 times)

Our task is to find the length of the shortest continuous subarray that has the same degree as the original array.

Algorithm Approach

We use a Map to track each element's first occurrence, last occurrence, and frequency. Then we find all elements with maximum frequency and determine which gives the shortest subarray.

Implementation

const findShortestSubArray = (arr = []) => {
    let range = new Map();
    let maxDegree = 0;
    let minLength = Infinity;
    
    // First pass: track frequency and positions
    for (let i = 0; i < arr.length; i++) {
        if (range.has(arr[i])) {
            let start = range.get(arr[i])[0];
            let degree = range.get(arr[i])[2];
            degree++;
            range.set(arr[i], [start, i, degree]);
            if (degree > maxDegree) {
                maxDegree = degree;
            }
        } else {
            let degree = 1;
            range.set(arr[i], [i, i, degree]);
            if (degree > maxDegree) {
                maxDegree = degree;
            }
        }
    }
    
    // Second pass: find shortest subarray with max degree
    for (let key of range.keys()) {
        let val = range.get(key);
        if (val[2] === maxDegree) {
            let diff = (val[1] - val[0]) + 1;
            if (diff < minLength) {
                minLength = diff;
            }
        }
    }
    
    return minLength;
};

const arr = [1, 2, 3, 3, 5, 6, 4, 3, 8, 3];
console.log("Shortest subarray length:", findShortestSubArray(arr));
Shortest subarray length: 8

How It Works

The algorithm works in two phases:

  1. Tracking Phase: For each element, we store [firstIndex, lastIndex, frequency] in a Map
  2. Finding Phase: Among elements with maximum frequency, we find the one with the smallest range (lastIndex - firstIndex + 1)

Example Walkthrough

// Let's trace through the example
const arr = [1, 2, 3, 3, 5, 6, 4, 3, 8, 3];
console.log("Element 3 positions: indices 2, 3, 7, 9");
console.log("Subarray from index 2 to 9: [3, 3, 5, 6, 4, 3, 8, 3]");
console.log("Length: 9 - 2 + 1 = 8");
Element 3 positions: indices 2, 3, 7, 9
Subarray from index 2 to 9: [3, 3, 5, 6, 4, 3, 8, 3]
Length: 9 - 2 + 1 = 8

Alternative Test Case

// Test with multiple elements having same max frequency
const arr2 = [1, 2, 2, 3, 1];
console.log("Array 2:", arr2);
console.log("Result:", findShortestSubArray(arr2));
console.log("Both 1 and 2 appear twice, but 2's range is shorter");
Array 2: [ 1, 2, 2, 3, 1 ]
Result: 2
Both 1 and 2 appear twice, but 2's range is shorter

Conclusion

This algorithm efficiently finds the shortest subarray with the same degree as the original array using a two-pass approach with O(n) time complexity. The key insight is tracking both frequency and positional information simultaneously.

Updated on: 2026-03-15T23:19:00+05:30

600 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements