Simple histogram generation of integer data in C#

后端 未结 4 1516
花落未央
花落未央 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:51

    You could use SortedDictionary

    uint[] items = new uint[] {5, 6, 1, 2, 3, 1, 5, 2}; // sample data
    SortedDictionary histogram = new SortedDictionary();
    foreach (uint item in items) {
        if (histogram.ContainsKey(item)) {
            histogram[item]++;
        } else {
            histogram[item] = 1;
        }
    }
    foreach (KeyValuePair pair in histogram) {
        Console.WriteLine("{0} occurred {1} times", pair.Key, pair.Value);
    }
    

    This will leave out empty bins, though

提交回复
热议问题