Sobes.tech
Junior — Senior

Calculating the total length of given segments

livecode

Task condition

You need to implement a function that calculates the total length of all intervals, considering their possible overlaps.

 const intervals = [
    [1, 2],
    [6, 10],
    [7, 9],
    [11, 15]
]

// Expected result — 9

The function should accept an array of pairs of numbers, where each pair represents the boundaries of an interval, and return the total length of the union of these segments.