Try looping through your array and checking whether array[x+1] = array[x] + 1. If true, skip to next number. When it hits false, save the string with the two endpoints and continue. Join all the strings at the end.