Grouping first and last value in array (Consecutive values are group with first and last element)

I have one object array with time intervals , Number 0 indicates sunday . In my time scheduling page selecting different time ranges in a particular day . I want to group the time values . My initial array is given below