Simple histogram generation of integer data in C#

后端 未结 4 1510
花落未央
花落未央 2020-12-31 19:44

As part of a test bench I\'m building, I\'m looking for a simple class to calculate a histogram of integer values (number of iterations taken for an algorithm to solve a pro

4条回答
  •  既然无缘
    2020-12-31 19:50

    You can use Linq:

    var items = new[] {5, 6, 1, 2, 3, 1, 5, 2};
    items
        .GroupBy(i => i)
        .Select(g => new {
            Item = g.Key,
            Count = g.Count()
        })
        .OrderBy(g => g.Item)
        .ToList()
        .ForEach(g => {
            Console.WriteLine("{0} occurred {1} times", g.Item, g.Count);
        });
    

提交回复
热议问题