Merge overlapping intervals

Developer
Size
1,889 Kb
Views
26,312

How do I make an merge overlapping intervals?

What is a merge overlapping intervals? How do you make a merge overlapping intervals? This script and codes were developed by Berkin on 10 September 2022, Saturday.

Merge overlapping intervals Previews

Merge overlapping intervals - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Merge overlapping intervals</title>
</head>
<body> <script src="js/index.js"></script>
</body>
</html>

Merge overlapping intervals - Script Codes JS Codes

var temp = [ [900, 1330], [1200, 1530], [1300, 1500], [1430, 1700], [1600, 1700], [1600, 1700], [1400, 1630], [1345, 1400], [1730, 1900], [1930, 2100], [2100, 2200]
]; // The main function that takes a set of intervals, merges // overlapping intervals and prints the result function mergeIntervals(intervals) { // Test if the given set has at least one interval if (intervals.length <= 0) return; // Create an empty stack of intervals var stack = [], last; // sort the intervals based on start time intervals.sort(function(a,b) { return a[0] - b[0]; }); console.log(intervals); // push the first interval to stack stack.push(intervals[0]); // Start from the next interval and merge if necessary for (var i = 1, len = intervals.length ; i < len; i++ ) { // get interval from last item last = stack[stack.length - 1]; // if current interval is not overlapping with stack top, // push it to the stack if (last[1] <= intervals[i][0]) { stack.push( intervals[i] ); } // Otherwise update the ending time of top if ending of current // interval is more else if (last[1] < intervals[i][1]) { last[1] = intervals[i][1]; stack.pop(); stack.push(last); } } console.log(stack); return; } mergeIntervals(temp);
Merge overlapping intervals - Script Codes
Merge overlapping intervals - Script Codes
Home Page Home
Developer Berkin
Username berkin
Uploaded September 10, 2022
Rating 3
Size 1,889 Kb
Views 26,312
Do you need developer help for Merge overlapping intervals?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Berkin (berkin) Script Codes
Create amazing Facebook ads with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!