JSFiddle - React, Tailwind, and code Playground
by Koray Orhun
JavaScript
var periods = [
{id: 'A', from: 7, to: 11},
{id: 'B', from: 2, to: 8},
{id: 'C', from: 9, to: 12},
{id: 'D', from: 4, to: 7},
{id: 'E', from: 10, to: 15},
{id: 'F', from: 0, to: 5},
{id: 'H', from: 5, to: 9},
{id: 'I', from: 11, to: 13},
{id: 'J', from: 7, to: 14},
];
var result = []; // result
function overlaps(a, b){
if(a.from > b.from){
return a.from < b.to || a.to < b.from || a.to < b.to;
}else if(a.from < b.from){
return a.from > b.to || a.to > b.from || a.to > b.to;
}else return true; // same starting, it automatically overlaps
}
var starting = (new Date()).getTime();
console.log('Starting at ' + starting);
var dic = {}
for(var i = 0; i<periods.length; i++){
var p = periods[i];
for(var j=p.from; j<p.to; j++)
if(!dic[j])
dic[j] = [] {i};
}
var ending = (new Date()).getTime();
console.log('Ended at ' + ending + ' (' + (ending-starting) + ' ms)');
document.write('<h1>' + result.length + ' periods overlap. Process duration : ' + (ending-starting) + ' ms</h1>')
console.log(result);