This is an assignment that I’ve been given, it says that if an hash-set contains the values {12, 13, 14, 23, 88, 89, 90, 91} then they would be represented
as the set of intervals { [12..14], [23..23], [88..91]}.
Now to the question, should I use for-each for this? I’m kind of confused since I’m not sure if you could group several intervals into 1 set, or should there be different hash-sets?
I did look into some methods like group-by but i don’t know if its the right one to use.
Advice or hints are appreciated!
I’d first sort them, and then iterate that ordered set, and combine elements, as long as the difference is only one.
Note that this requires distinct elements in the input. If the input is a hashset, that’s guaranteed. Else throw in a
Distict()call before callingOrderBy.