Rates TodayNIFTY25,670-0.83%SENSEX83,630.7-0.83%Gold 24K ₹11,320.24/g down186.46Silver ₹134.77/g down2.72
InsightforGeeks

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.

JavaScript

Stuck?