Merge intervals
MediumArrays · JavaScript
The task
Write mergeIntervals(intervals) where intervals is an array of [start, end] pairs.
Merge every group of overlapping or touching intervals and return the result sorted by start. For example [1,3] and [2,6] merge into [1,6]; [1,4] and [4,5] also merge, because they touch.
Examples
- mergeIntervals([[1,3],[2,6],[8,10]]) returns [[1,6],[8,10]]
Your code runs in your browser, in a separate worker, and is never sent to us. Edit the function, then press Run tests. Your work is saved here as you type.
JavaScriptCtrl or Cmd + Enter to run