Q:

How to split a list of DateTime based on the time differences found within the entries?

I have a list that contains deserialized objects, and within those objects is a DateTime field. The times goes up sequentially in seconds but there will be instances where there is a time differnce of more than 4 seconds from one entry to the next, and then continues from there. 
/* List example:

    26/11/2019 10:26:01
    26/11/2019 10:26:02
    26/11/2019 10:26:03
    26/11/2019 10:26:04
    26/11/2019 10:26:30 // difference detected, subdivide into new list
    26/11/2019 10:26:31
    26/11/2019 10:26:40 // difference detected, subdivide into new list
    26/11/2019 10:26:41
    26/11/2019 10:26:42
    26/11/2019 10:26:43
*/

So what I want to do is iterate through this big list and then sub-divide those entries into smaller list but I'm unsure how to do this. My initial thought would be to create a for-loop and then individually compare the datetimes to see if there's a 4 second difference, but I can already tell that will take forever. 
Would like some suggestions.
Thanks

A:

If the list is big, a for loop is the best approach in terms of performance.
If you need even more performance consider working with arrays and using Array.Copy() which is very fast in copying subarrays or the whole array.
private List<DateTime[]> SplitMe(List<DateTime> bigList)
{
    var splitArrays = new List<DateTime[]>();
    var bigArray = bigList.ToArray();

    var previousItem = bigList[0];
    int lastEndingIndex = -1;
    for (int index = 1; index < bigArray.Length; index++)
    {
        var currentItem = bigArray[index];

        if (currentItem < previousItem + TimeSpan.FromSeconds(4))
        {
            var newArray = new DateTime[index - lastEndingIndex];
            Array.Copy(bigArray, lastEndingIndex + 1, newArray, 0, index - lastEndingIndex);
            splitArrays.Add(newArray);
            lastEndingIndex = index;
        }

        previousItem = currentItem;
    }

    return splitArrays;
}

If you do not like the array approach, you can use the much slower LINQ.
bigList.Skip(lastEndingIndex + 1).Take(index - lastEndingIndex).ToList() will do the job.

