C# { //在字典中按从高到低的频率排列对。 var sorted=从频率对中排序 按对排序。值递减 选择配对; //按顺序显示所有结果。 int MaxNumValues=sorted.First().Value; foreach(排序中的变量对) { int numValues=pair.Value; if(numValuesitem.Key) .ToDictionary(item=>item.Key,item=>item.Value); } 静态无效显示分类频率(字典频率) { foreach(频率中的var对) { Console.WriteLine($“{pair.Key}={pair.Value}”); } }
无论如何,如果代码必须执行,我会选择已经发布的使用Linq的解决方案。如果您不想使用Linq,出于某种原因,我建议您先对项目进行排序,然后在一行中计算相等的元素,而不是对每个元素使用字典搜索。我对您的代码做了一些更改C# { //在字典中按从高到低的频率排列对。 var sorted=从频率对中排序 按对排序。值递减 选择配对; //按顺序显示所有结果。 int MaxNumValues=sorted.First().Value; foreach(排序中的变量对) { int numValues=pair.Value; if(numValuesitem.Key) .ToDictionary(item=>item.Key,item=>item.Value); } 静态无效显示分类频率(字典频率) { foreach(频率中的var对) { Console.WriteLine($“{pair.Key}={pair.Value}”); } },c#,C#,无论如何,如果代码必须执行,我会选择已经发布的使用Linq的解决方案。如果您不想使用Linq,出于某种原因,我建议您先对项目进行排序,然后在一行中计算相等的元素,而不是对每个元素使用字典搜索。我对您的代码做了一些更改 static void Main(string[] args) { // Compute frequencies for this data. string[] values = { "bir
static void Main(string[] args)
{
// Compute frequencies for this data.
string[] values = { "bird", "cat", "bird", "dog", "bird", "man", "frog", "cat" };
var freqs = GetFrequencies(values);
DisplaySortedFrequencies(freqs);
}
static Dictionary<string, int> GetFrequencies(IEnumerable<string> values)
{
if (values == null) return new Dictionary<string, int>();
var maxCount = 1;
var result = new Dictionary<string, int>();
foreach (string value in values)
{
if (result.TryGetValue(value, out int count))
{
result[value] = count + 1;
if (maxCount < result[value])
{
maxCount = result[value];
}
}
else
{
result.Add(value, 1);
}
}
return result
.Where(item => item.Value == maxCount)
.OrderBy(item => item.Key)
.ToDictionary(item => item.Key, item => item.Value);
}
static void DisplaySortedFrequencies(Dictionary<string, int> frequencies)
{
foreach (var pair in frequencies)
{
Console.WriteLine($"{pair.Key} = {pair.Value}");
}
}
static void Main(字符串[]args)
{
//计算此数据的频率。
字符串[]值={“鸟”、“猫”、“鸟”、“狗”、“鸟”、“人”、“青蛙”、“猫”};
var freqs=获取频率(值);
显示分类频率(频率);
}
静态字典GetFrequencies(IEnumerable值)
{
if(value==null)返回新字典();
var maxCount=1;
var result=newdictionary();
foreach(值中的字符串值)
{
if(result.TryGetValue(value,out int count))
{
结果[值]=计数+1;
如果(最大计数<结果[值])
{
maxCount=结果[值];
}
}
其他的
{
结果:增加(值1);
}
}
返回结果
.Where(item=>item.Value==maxCount)
.OrderBy(item=>item.Key)
.ToDictionary(item=>item.Key,item=>item.Value);
}
静态无效显示分类频率(字典频率)
{
foreach(频率中的var对)
{
Console.WriteLine($“{pair.Key}={pair.Value}”);
}
}
我对您的代码做了一些更改
static void Main(string[] args)
{
// Compute frequencies for this data.
string[] values = { "bird", "cat", "bird", "dog", "bird", "man", "frog", "cat" };
var freqs = GetFrequencies(values);
DisplaySortedFrequencies(freqs);
}
static Dictionary<string, int> GetFrequencies(IEnumerable<string> values)
{
if (values == null) return new Dictionary<string, int>();
var maxCount = 1;
var result = new Dictionary<string, int>();
foreach (string value in values)
{
if (result.TryGetValue(value, out int count))
{
result[value] = count + 1;
if (maxCount < result[value])
{
maxCount = result[value];
}
}
else
{
result.Add(value, 1);
}
}
return result
.Where(item => item.Value == maxCount)
.OrderBy(item => item.Key)
.ToDictionary(item => item.Key, item => item.Value);
}
static void DisplaySortedFrequencies(Dictionary<string, int> frequencies)
{
foreach (var pair in frequencies)
{
Console.WriteLine($"{pair.Key} = {pair.Value}");
}
}
static void Main(字符串[]args)
{
//计算此数据的频率。
字符串[]值={“鸟”、“猫”、“鸟”、“狗”、“鸟”、“人”、“青蛙”、“猫”};
var freqs=获取频率(值);
显示分类频率(频率);
}
静态字典GetFrequencies(IEnumerable值)
{
if(value==null)返回新字典();
var maxCount=1;
var result=newdictionary();
foreach(值中的字符串值)
{
if(result.TryGetValue(value,out int count))
{
结果[值]=计数+1;
如果(最大计数<结果[值])
{
maxCount=结果[值];
}
}
其他的
{
结果:增加(值1);
}
}
返回结果
.Where(item=>item.Value==maxCount)
.OrderBy(item=>item.Key)
.ToDictionary(item=>item.Key,item=>item.Value);
}
静态无效显示分类频率(字典频率)
{
foreach(频率中的var对)
{
Console.WriteLine($“{pair.Key}={pair.Value}”);
}
}
这是最常见的,OP希望所有数字出现不止一次,或者如果没有数字出现不止一次,则希望所有数字都出现。@juharr感谢您指出这一点,请参阅最常见的编辑,OP希望所有数字出现不止一次,或者如果没有数字出现不止一次,则希望所有数字都出现。@juharr感谢您指出这一点,请参阅editI删除了visual studio标记,因为这些标记是为有关开发环境的问题保留的。但是,您的问题只是关于C语言。我删除了visual studio标记,因为这些标记是为有关开发环境的问题保留的。然而,你的问题只是关于C语言。
int appearsMost = new int[] { 1, 2, 3, 4, 3, 3, 2, 2, 4 }
.GroupBy(x => x)
.Select(x => (Key: x.Key, Items: x.ToList()))
.OrderByDescending(x => x.Items.Count)
.First().Key;
int[] appearMost = new int[] { 1, 2, 3, 4, 3, 3, 2, 2, 4 }
.GroupBy(x => x)
.Select(x => (Key: x.Key, Items: x.ToList()))
.GroupBy(x => x.Items.Count)
.OrderByDescending(x => x.Key)
.First()
.Select(x => x.Key)
.ToArray();
int[] appearMoreThanOnce = new int[] { 1, 2, 3, 4, 3, 3, 2, 2, 4 }
.GroupBy(x => x)
.Select(x => (Key: x.Key, Items: x.ToList()))
.OrderByDescending(x => x.Items.Count)
.Where(x => x.Items.Count >= 1).Select(x => x.Key).ToArray();
string[] animalsThatAppearMoreThanOnce = new string[] { "bird", "cat", "bird", "dog", "bird", "man", "frog", "cat" }
.GroupBy(x => x)
.OrderByDescending(x => x.Count())
.Where(x => x.Count() >= 1).Select(x => x.Key).ToArray();
// I added another cat, so this will return 'bird' and 'cat'.
string[] animalsThatAppearMost = new string[] { "bird", "cat", "bird", "dog", "bird", "man", "frog", "cat", "cat" }
.GroupBy(x => x)
.Select(x => (Key: x.Key, Items: x.ToList()))
.GroupBy(x => x.Items.Count)
.First()
.Select(x => x.Key)
.ToArray();
public int[] MostCommon(int[] numbers)
{
var ans = numbers
.GroupBy(x => x)
.Select(x => new {x.Key, x.Count}))
.Where(x => x.Count > 1)
.Select(x => x.Key)
.ToArray();
return ans.Length > 0 ? ans : numbers;
}
var maxFrequency = sorted.First().Value;
Console.WriteLine("These items all occur the most:");
foreach (var pair in sorted)
{
if (pair.Value < maxFrequency) break;
Console.WriteLine($" - {pair.Key} = {pair.Value}");
}
static void Main(string[] args)
{
// Compute frequencies for this data.
string[] values = { "bird", "cat", "bird", "dog", "bird", "man", "frog", "cat" };
var freqs = GetFrequencies(values);
DisplaySortedFrequencies(freqs);
}
static Dictionary<string, int> GetFrequencies(IEnumerable<string> values)
{
if (values == null) return new Dictionary<string, int>();
var maxCount = 1;
var result = new Dictionary<string, int>();
foreach (string value in values)
{
if (result.TryGetValue(value, out int count))
{
result[value] = count + 1;
if (maxCount < result[value])
{
maxCount = result[value];
}
}
else
{
result.Add(value, 1);
}
}
return result
.Where(item => item.Value == maxCount)
.OrderBy(item => item.Key)
.ToDictionary(item => item.Key, item => item.Value);
}
static void DisplaySortedFrequencies(Dictionary<string, int> frequencies)
{
foreach (var pair in frequencies)
{
Console.WriteLine($"{pair.Key} = {pair.Value}");
}
}