JSFiddle - React, Tailwind, and code Playground

by Prathameshsb

CSS

*{
  background-color: #2c2c2c;
}

JavaScript

function test(intervals) {
	intervals = intervals.sort((a,b) => a[0] - b[0]);
  
  let result = []; let temp = intervals[0];
  for(let i = 0; i < intervals.length; i++) {
  	if(intervals[i][0] <= temp[1]) {
    	temp[1] = Math.max(intervals[i][1], temp[1]);
    }else {
    	result.push(temp);
      temp = intervals[i];
    }
  }
  result.push(temp);
  return result;
}

console.log(test([[1,3],[2,6],[8,10],[15,18]]))