30秒学会 C# 片段 · 2018年12月28日

30秒学会 C# 片段 – Frequencies

Returns a Dictionary with the unique values of a collection as keys and their frequencies as the values.

Use IEnumerable.GroupBy() to create groups for each distinct value in the collection.
Use IEnumerable.ToDictionary() to convert the result of the previous operation to a Dictionary.

代码实现

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

public static partial class _30s 
{
  public static Dictionary<T,int> Frequencies<T>(IEnumerable<T> values)
  {
    return values
      .GroupBy(v => v)
      .ToDictionary(v => v.Key, v => v.Count());
  }
}

使用样例

char[] c = {'a', 'b', 'a', 'c', 'a', 'a', 'b'}; 

_30s.Frequencies(c); // { [a, 4], [b, 2], [c, 1] }