javascript - how to check overlapping of multiple number ranges injavascript -
i have problem in javascript in need determine if set of number ranges has overlapping. examples :
1 - 5, 4 - 6, 7 - 8 (has overlap)
1 - 5, 6 - 8, 9 - 12 (has no overlap)
thanks in advance!
the set of ranges must sorted first, starting element greater(not less than) previous starting number.in case sorted. every range check whether starting number greater previous ending number. if true ranges, non overlapping.
var ranges=[[1,5],[4,6],[7,8]]; ranges.sort() // sorting done respect first element in array. for(var i=1;i<ranges.length;i++){ //start second element , compare first if(ranges[i][0]<=ranges[i-1][1]) break; } if(i==ranges.length) console.log("non overlapping") else console.log("overlapping")
Comments
Post a Comment