Merge Intervals

Merge overlapping intervals into consolidated ranges, a classic algorithm for scheduling and resource allocation.

Code

Algorithms
echo "$intervals" | jq 'sort_by(.[0]) | reduce .[] as $i ([]; if length == 0 then [$i] elif .[-1][1] >= $i[0] then .[-1][1] = ([.[-1][1], $i[1]] | max) else . + [$i] end)'

Parameters

Array of [start, end] intervals (JSON)

Server

More Bash Snippets