Dev Helpers
← Back

Merge overlapping meeting slots

Merge overlapping meeting slots using JavaScript.

Open this example in the interactive library

Source code

const slots = [
  [9, 11],
  [10, 12],
  [14, 15],
].toSorted((a, b) => a[0] - b[0]);
const merged = [];
for (const [start, end] of slots) {
  const last = merged.at(-1);
  if (last && start <= last[1]) last[1] = Math.max(last[1], end);
  else merged.push([start, end]);
}
console.log(merged);

Sample output

[ [ 9, 12 ], [ 14, 15 ] ]