I have a list of dates organized like this:
(From, To)
(From, To)
...
(From, To)
I am trying to find how to consolidate ranges in an efficient way (it has to be quite fast because it is to consolidate financial data streams in realtime).
Dates do NOT overlap.
what I was thinking about is:
Sort everything by From time and then iterate through pairs to see if Pair1.To == Pair2.From to merge them, but this means several iterations.
Is there a better way to do this, like in a single pass
Here are some examples
(2019-1-10, 2019-1-12)
(2019-3-10, 2019-3-14)
(2019-1-12, 2019-1-13)
expected output:
(2019-1-10, 2019-1-12) + (2019-1-12, 2019-1-13) -> (2019-1-10, 2019-1-13)
(2019-3-10, 2019-3-14) -> (2019-3-10, 2019-3-14)
In practice, it's really about seconds and not dates, but the idea is the same.