Removing already listed intervals in JavaScript

JavaScript function that takes in a 2-D array, arr, as the first and the only argument.

Each subarray of our input array is an array of exactly two numbers, specifying a time interval.

Our function should remove all intervals that are covered by another interval in the array arr. Interval [a,b) is covered by interval [c,d) if and only if c

Problem Example

For example, if the input to the function is:

const arr = [
    [2, 5],
    [5, 7],
    [3, 9]
];

Then the output should be:

2

Output Explanation

Interval [5, 7] is covered by [3, 9], therefore it is removed. The interval [3, 9] covers [5, 7] because 3

Solution

The algorithm works by first sorting the intervals, then iterating through them to identify covered intervals:

const arr = [
    [2, 5],
    [5, 7],
    [3, 9]
];

const removeCovered = (arr = []) => {
    // Sort by start time, if equal then by end time (descending)
    arr.sort(([a, b], [c, d]) => (a === c ? d - b : a - c));
    
    let last = arr[0];
    let count = arr.length;
    
    for(let i = 1; i < arr.length; i++){
        const [a, b] = last;
        const [c, d] = arr[i];
        
        // Check if current interval is covered by the last interval
        if(c >= a && d <= b){
            count -= 1;
        } else {
            last = arr[i];
        }
    }
    
    return count;
};

console.log(removeCovered(arr));
2

How It Works

The solution follows these steps:

  1. Sort intervals: Sort by start time first. If start times are equal, sort by end time in descending order.
  2. Track last valid interval: Keep track of the most recent interval that wasn't covered.
  3. Check coverage: For each interval, check if it's covered by the last valid interval.
  4. Count remaining: Decrement count when a covered interval is found.

Step-by-Step Example

const arr = [[2, 5], [5, 7], [3, 9]];

// After sorting: [[2, 5], [3, 9], [5, 7]]
console.log("Original:", arr);

const removeCoveredDetailed = (arr = []) => {
    arr.sort(([a, b], [c, d]) => (a === c ? d - b : a - c));
    console.log("After sorting:", arr);
    
    let last = arr[0];
    let count = arr.length;
    console.log("Starting with interval:", last);
    
    for(let i = 1; i < arr.length; i++){
        const [a, b] = last;
        const [c, d] = arr[i];
        
        console.log(`Checking [${c}, ${d}] against [${a}, ${b}]`);
        
        if(c >= a && d <= b){
            console.log(`  [${c}, ${d}] is covered by [${a}, ${b}]`);
            count -= 1;
        } else {
            console.log(`  [${c}, ${d}] is not covered, updating last`);
            last = arr[i];
        }
    }
    
    return count;
};

console.log("Result:", removeCoveredDetailed(arr));
Original: [ [ 2, 5 ], [ 5, 7 ], [ 3, 9 ] ]
After sorting: [ [ 2, 5 ], [ 3, 9 ], [ 5, 7 ] ]
Starting with interval: [ 2, 5 ]
Checking [3, 9] against [2, 5]
  [3, 9] is not covered, updating last
Checking [5, 7] against [3, 9]
  [5, 7] is covered by [3, 9]
Result: 2

Conclusion

This solution efficiently removes covered intervals by sorting and single-pass comparison. The time complexity is O(n log n) due to sorting, making it optimal for this interval coverage problem.

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

213 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements