I am looking for best practice to count how many times each date appears in a list.
At the moment, I have working code (just tested), but I think the way I did is not so good.
var dates = new List<DateTime>(); //Fill list here var dateCounter = new Dictionary<DateTime, int>(); foreach (var dateTime in dates) { if (dateCounter.ContainsKey(dateTime)) { //Increase count dateCounter[dateTime] = dateCounter[dateTime] + 1; } else { //Add to dictionary dateCounter.Add(dateTime, 1); } }
Who knows the best solution?
source share