Is there a specialized algorithm to collapse (to aggregate) the missing numbers in the set? -
that array of integer numbers [3, 4, 5, 10, 10000] transform array / list ['1, 2', 3, 4, 5, '6-9', 10, '11-9999', 10000]. small example, clear loop through numbers 1 10000 not effective.
assuming array sorted, boils down to:
for = 0 ... array.length-1 write array[i] switch( array[i+1] - array[i]) case 1: nothing; case 2: write "'" + (array[i]+1) + "'"; case 3: write "'" + (array[i]+1) + " , " + (array[i]+2) + "'" default: write "'" + (array[i]+1) + " - " + (array[i+1]-1) + "'" write array[array.length] note: various switch stick "presentation" of new array missing items; if that's not necessary, pout if(array[i+1]-array[i] > 1)
Comments
Post a Comment