While working on my angular app, I encountered a seemingly simple task that turned out to be surprisingly complex. I have come up with a few solutions, but none of them seem perfect.
The issue at hand is that I have an array containing possible unlimited(limited) number of [start, end] values, representing number ranges. For example:
const rangeArr =[[5,10], [15,20], [9,14]];
Now, I need a function that can check for overlaps between any of these ranges. In the given example, the last range [9,14] overlaps with the first one [5,10]
I am currently using a reduce method like this to find an index of an overlapping array:
rangeArr.findIndex((range:[number,number], index: number, arr:[number,number][])=>{
for(let i = 0; i < arr.length-1;i++){
if(index === i)continue;
if(range[0]>=arr[index][0] && range[0]<=arr[index][1]){
return true;
}
if(range[1]>=arr[index][0] && range[1]<=arr[index][1]){
return true;
}
return false;
}
})
Although I opted for a traditional approach...I believe there must be a simpler and more concise solution out there...