For a given list of different numbers from 1 to N, you need to build an abbreviated version of the list: if there are several numbers in a row, they are replaced with an interval through the dash. Example:
1,3,4,5,6,7,8,10,12,16,17,20,21,22,23,24
turns into
1,3-8,10,12,16-17,20-24
How to implement it in the most elegant way?
The specific language is not important, but better than C ++ or similar.