Creating a catalogue index You have a program which creates an index for a catalogue.  The last step is missing: it must find and consolidate page ranges within the list of pages for each entry.

Each entry is currently an array of unique integers in order, e.g:

[18,19,20,21,22,47,56,57,62,67,68,69,70]


Write a function which returns a consolidated array of ranges and individual pages as appropriate.

Individual pages can be converted to strings or remain as integers.  Either of these would be valid results from the example input:

["18-22",47,"56-57",62,"67-70"] // valid
["18-22","47","56-57","62","67-70"] // valid
Mathematica, 74

Assuming that replacing [] with {} in I/O is allowed:

t=ToString;f=Split[#,#2&lt;#+2&amp;]/.{s_Integer,___,e_}:&gt;t@s&lt;&gt;"-"&lt;&gt;t@e/.{x_}:&gt;x&amp;



  f[{18, 19, 20, 21, 22, 47, 56, 57, 62, 67, 68, 69, 70}]
  
  {"18-22", 47, "56-57", 62, "67-70"}


The Split part should be quite fast (because someone else probably spent a lot of time optimizing) - the pattern matching part might be a bit slower in comparison. If speed is the main criterium, then there will be other ways to process the Split result (let´s see how the Q develops). For an input of 10^6 entries this takes about 2.7s on my (average) laptop (2.2GHz i7), of which about 1s goes to Split.