In a list of elements I want to group the elements which are in consecutive order. For eg.
list = {2,3,4,5,6,9,12,13,14,16,17}
result = {{2,3,4,5,6},{9},{12,13,14},{16,17}}
I have written the code below for it and it works fine. I wanted to know if I can use any in built Function to do it in a better way.
For[
i = 1; res = {},
i <= Length[list],
i++,
temp = {list[[i]]} ; While[If[i + 1 > Length[list], Break[]];list[[i + 1]] - list[[i]] == 1,AppendTo[temp, list[[i + 1]]]; i++]; AppendTo[res, temp]
];