...

/

Introduction to Merge Intervals

Introduction to Merge Intervals

Let’s go over the Merge Intervals pattern, its real-world applications, and some problems we can solve with it.

About the pattern

The merge intervals pattern is all about working with time ranges that might overlap. Each time range, or interval, is defined by a start and an end point—for example, [10, 20] means the range starts at 10 and ends at 20. The pattern works by identifying overlapping intervals and merging them into one. If there is no overlap, the interval is kept as it is. Let’s take a look at the illustration to help visualize this.

Press + to interact

Before we can use this pattern, we need to know how to spot overlapping intervals. So, how can we tell if two intervals overlap? Let’s look at the illustration below, which shows the different ways two intervals can relate to each other:

Press + to interact
canvasAnimation-image
1 / 3

As per the illustration above, two intervals overlap if the start time of one interval is less than or equal to the end time of the other interval.

Now, let’s see how the Merge ...