find the average of all values in a map in csharp

Assuming the map is a Dictionary<TKey, TValue>, you can use LINQ to calculate the average of all the values:

main.cs
using System.Linq;

Dictionary<int, int> map = new Dictionary<int, int>();
// Add values to the map

double average = map.Values.Average();
140 chars
7 lines

This code uses the Average extension method provided by LINQ to calculate the average of the values in the Dictionary. Note that Values is a property of the Dictionary that returns a collection of all the values. If the Dictionary contains a different type of value, you may need to cast the values to double or another appropriate type before calculating the average.

gistlibby LogSnag