C#MODE查找问题 [英] C# MODE Finding Problem

查看:70
本文介绍了C#MODE查找问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!



请告诉我如何在C#中找到模式



如果数组包含这些数字:



array [10] = {1,2,3,4,3,6,7,6, 7,3,}



如果你有我能理解的好解,请告诉我。

Hello Everyone!

Please Tell Me How To Find Mode In C#,

IF Array Contain These Number:"

array[10]={1,2,3,4,3,6,7,6,7,3,}

Please Tell Me If Your Have Good Solution Which I Can understand.

推荐答案

由于模式是样本中频率最高的数字(在您的示例中为3),您可以通过多种方式进行操作。

您可以对数组进行排序,然后对元素进行计数。找到最大的数量,它会告诉你它是哪个数字。排序是内置的,计数/查找最大只是一个简单的循环。

或者,你可以使用Linq:

Since the Mode is the number with the greatest frequency in the sample (3 in your example) you can do it in a number of ways.
You could sort the array, and then count the elements. Find the largest count, and that tells you which number it is. Sort is built in, and counting / find largest is just a simple loop.
Or, you could use Linq:
int mode = myArray.GroupBy(i => i).OrderByDescending(g => g.Count()).First().Key;



但是......那不会给你带来任何作业要点......:笑:



试一试:这并不困难!


But...that won't get you any homework points...:laugh:

Give it a try: this isn't difficult!


这篇关于C#MODE查找问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆