如何在列表中找到最常见的数字 [英] How to find the most common number in list

查看:84
本文介绍了如何在列表中找到最常见的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我想编写显示学校最常见课程的方法。

I want to code method which shows the most common classes in school.




我在Agency  csv文件中有列表,该文件读取完美。问题,我不知道如何制作简单的程序,将找到列表中最常见的数字。现在代码只读取和比较列表中的相同数字并输出"studentNumber"。
这是所有列表。

I have list in Agency  csv file, which reads perfectly. Problem that I don't know how to make simple program which would find the most common number in the list. Now the code only reads and compares the same numbers as in list and output "studentNumber" which is  all the list.

public static void mostCommon(Agency[] Agency, int studentNumber, out int commonStudent,

out int tempNumber)
{
commonStudent = 0;
tempNumber = 0;




for(int i = 0; i< studentNumber; i ++)
{
tempNumber = Agency [i]。类;
if(tempNumber == Agency [i] .Class)
{
commonStudent ++;
}

}
}

out int tempNumber) { commonStudent = 0; tempNumber = 0; for ( int i = 0; i < studentNumber ; i++) { tempNumber = Agency[i].Class; if (tempNumber == Agency[i].Class) { commonStudent++; } } }




$




  &NBSP; &NBSP; &NBSP;&NBSP;





        




推荐答案

你好,

 除非需要,否则我通常不会提供代码来回复发帖。但是,

 I normally do not provide code to answer posting unless its required. However,

如果使用Container类,如Dictionary,对象为int和int。 此

if you use the Container Class such as Dictionary, with objects int and int.  This

Container对象允许使用唯一的学生班级代码和每个

Container object allows for unique Student Class Code and a count for each

学生班级代码请求的计数。 然后编写循环,foreach或while等编码,并

request of student Class Code.  Then code a loop, foreach or while, etc. and

从cvs文件中记录学生类代码。 在

record the Student Class Code from your cvs file.  Read the entire file while

执行以下操作时读取整个文件;

doing the following;

 检查预先录制的学生课程代码

 Check for prerecorded Student Class Codes

  如果为true则调整计数

   If true then adjust count

  如果为false,则将Student Class Code添加到Dictionary对象。

   if false then add the Student Class Code to the Dictionary object.

 从任何循环内部更改TValue会有点棘手。请记住,

 This will be somewhat tricky to change TValue from inside any loop. Remember,

在foreach执行时,您无法添加/删除项目。我建议使用标准

you can not Add/Remove items while foreach executes. I recommend standard

For或控制While循环。这是MSDN阅读的链接,我建议

For or controlled While loops. Here is a link to the MSDN for reading, I suggest

您阅读有关此Container类的信息;

you read the information about this Container Class;

 

https://msdn.microsoft.com/en-us/library/xfhwa508(v = vs.80).aspx

https://msdn.microsoft.com/en-us/library/xfhwa508(v=vs.80).aspx

 

如果您需要进一步的帮助,请告诉我们您遇到的任何问题。

If you require further assistance please let us know and any problems you run in to.

此外,还有许多编码方法可以解决您的请求。全取决于你。

Also, there are many methods of coding to solve your request. Its all up to you.

 希望这有助于:)

 Hope this Helps :)


这篇关于如何在列表中找到最常见的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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