Finding minimum time difference in an array in JavaScript

We are required to write a JavaScript function that takes in an array of 24-hour clock time points in "Hour:Minutes" format. Our function should find the minimum minutes difference between any two time points in the array.

For example, if the input to the function is:

const arr = ["23:59","00:00"];

Then the output should be 1 minute, because the minimum difference between these times is 1 minute (from 23:59 to 00:00).

Understanding the Problem

The key challenge is handling the circular nature of time. When comparing "23:59" and "00:00", we need to consider that 00:00 is just 1 minute after 23:59, not 23 hours and 59 minutes before.

Solution Approach

Our approach involves:

  • Convert all times to minutes from 00:00
  • Handle the circular nature by considering both direct differences and wrap-around differences
  • Find the minimum difference among all pairs

Example Implementation

const arr = ["23:59","00:00"];

const findMinDifference = (arr = []) => {
    const find = (str = '') => str.split(':').map(time => parseInt(time, 10))
    const mapped = arr.map((time) => {
        const [hour1, minute1] = find(time)
        return hour1 * 60 + minute1
    });
    
    const sorted = []
    let isrepeating = false
    
    mapped.forEach((time) => {
        if (sorted[time] !== undefined || sorted[time + 24 * 60] !== undefined) {
            isrepeating = true
        }
        sorted[time] = time
        sorted[time + 24 * 60] = time + 24 * 60
    })
    
    if (isrepeating) {
        return 0
    }
    
    let min = Infinity
    let prev = null
    
    for (let i = 0; i < sorted.length; i++) {
        if (sorted[i] !== undefined) {
            if (prev) {
                min = Math.min(min, sorted[i] - prev)
            }
            prev = sorted[i]
        }
    }
    
    return min
};

console.log(findMinDifference(arr));
1

How It Works

The algorithm works by:

  1. Converting times to minutes: "23:59" becomes 1439 minutes, "00:00" becomes 0 minutes
  2. Handling duplicates: If any time appears twice, the minimum difference is 0
  3. Creating circular timeline: Each time is stored twice - once as is, and once with 24*60 minutes added
  4. Finding minimum: Iterate through the sorted times and find the smallest gap

Testing with More Examples

// Test with multiple time points
const test1 = ["12:30", "14:45", "16:20"];
console.log("Test 1:", findMinDifference(test1)); // 95 minutes

const test2 = ["00:00", "12:00", "23:59"];
console.log("Test 2:", findMinDifference(test2)); // 1 minute

const test3 = ["05:30", "05:30"];
console.log("Test 3:", findMinDifference(test3)); // 0 (duplicate)
Test 1: 95
Test 2: 1
Test 3: 0

Conclusion

This solution efficiently finds the minimum time difference by converting times to minutes and handling the circular nature of the 24-hour clock. The algorithm correctly handles edge cases like duplicate times and wrap-around scenarios.

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

468 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements