a 8cbad025ee 1 2 weeks ago
..
CompanyCoreLib 8cbad025ee 1 2 weeks ago
ConsoleApp1 8cbad025ee 1 2 weeks ago
1.jpg 8cbad025ee 1 2 weeks ago
readme.md 8cbad025ee 1 2 weeks ago

readme.md

Эщкерембус

Program.cs

using CompanyCoreLib;
using System;
using System.Collections.Generic;
using System.Linq; 

// создаём экземпляр класса аналитики
var analytics = new Analytics();

// подгатавливаем массив тестовых данных
var srcDates = new List<DateTime>()
{
    new DateTime(2020,11,01,0,0,0),
    new DateTime(2032,05,03,0,0,0),
    new DateTime(2023,12,10,0,0,0),
    new DateTime(2023,11,11,0,0,0),
    new DateTime(2023,11,09,0,0,0),
};

// выполняем метод и получаем результат
var outDates = analytics.PopularMonths(srcDates);
var sortedDates = srcDates.OrderByDescending(date => date).ToList();

// выводим результат в консоль
foreach (var date in sortedDates)
{
    Console.WriteLine(date.ToString());
}

Analytics.cs

namespace CompanyCoreLib
{
    public class Analytics
    {
        public List<DateTime> PopularMonths(List<DateTime> dates)
        {
            var dateTimeCounterDictionary = new Dictionary<DateTime, int>();
            int previousYear = DateTime.Now.Year - 1;

            foreach (DateTime iterDate in dates)
            {
                if (iterDate.Year == previousYear)
                {
                    // вычисляем начало месяца для текущей даты
                    var dateMonthStart = new DateTime(
                        iterDate.Year,  // год
                        iterDate.Month, // месяц
                        1);             // день

                    if (dateTimeCounterDictionary.ContainsKey(dateMonthStart))
                    {
                        dateTimeCounterDictionary[dateMonthStart]++;
                    }
                    else
                    {
                        dateTimeCounterDictionary[dateMonthStart] = 1;
                    }
                }
            }

            return dateTimeCounterDictionary
                .OrderByDescending(pair => pair.Value)
                .ThenBy(pair => pair.Key)               
                .Select(pair => pair.Key)               
                .ToList();                              
        }
    }
}