using System.Collections.Generic;
public class ConsecutiveDates
public DateTime StartDate;
public static void Main()
List<ConsecutiveDates> datesList = new List<ConsecutiveDates>();
datesList.Add(new ConsecutiveDates{userId = 0, StartDate = new DateTime(2022, 8, 20), EndDate = new DateTime(2022, 8, 23)});
datesList.Add(new ConsecutiveDates{userId = 1, StartDate = new DateTime(2022, 8, 20), EndDate = new DateTime(2022, 8, 22)});
datesList.Add(new ConsecutiveDates{userId = 2, StartDate = new DateTime(2022, 8, 20), EndDate = new DateTime(2022, 8, 26)});
datesList.Add(new ConsecutiveDates{userId = 3, StartDate = new DateTime(2022, 8, 20), EndDate = new DateTime(2022, 8, 23)});
var compareRanges = datesList.Select(d => GetDateRange(d)).ToList();
var result = CompareDates(compareRanges, 1);
Console.WriteLine("Overlaps");
foreach(var dt in result)
Console.WriteLine(dt.ToString("M/d/yyyy"));
var maxOverlap = GetLongestOverlap(compareRanges);
Console.WriteLine("Most overlaps: " + maxOverlap.Item1.Count());
foreach(var dt in maxOverlap.Item1)
Console.WriteLine(dt.ToString("M/d/yyyy"));
private static DateTime[] GetDateRange(ConsecutiveDates dt)
var start = dt.StartDate.Date;
var end = dt.EndDate.Date;
return Enumerable.Range(0, Convert.ToInt32((end.Date - start.Date).TotalDays) + 1)
.Select(e => start.Date.AddDays(e)).ToArray();
private static List<DateTime> CompareDates(List<DateTime[]> compareRanges, int overlapThreshold = 1)
var grouped = compareRanges.SelectMany(r => r).GroupBy(d => d.Date);
var thresholdMatch = grouped.Where(g => g.Count() >= overlapThreshold)
private static Tuple<List<DateTime>, int> GetLongestOverlap(List<DateTime[]> compareRanges)
var grouped = compareRanges.SelectMany(r => r).GroupBy(d => d.Date);
var max = grouped.Max(g => g.Count());
var maxMatch = grouped.Where(g => g.Count() == max)
return new Tuple<List<DateTime>, int>(maxMatch, max);