我有这个数组我写了一个函数MostFreq,它接受一个整数数组并返回2个值:数组中更频繁的数字及其频率检查这段代码我觉得你觉得怎么样?有更好的方法吗?
static void Main()
{
int [] M={4,5,6,4,4,3,5,3};
int x;
int f=MyMath.MostFreq(M,out x );
console.WriteLine("the most Frequent Item = {0} with frequency = {1}",x,f);
}
Run Code Online (Sandbox Code Playgroud)
=====
在Mymath班
public static int MostFreq(int[] _M, out int x)
{
//First I need to sort the array in ascending order
int Max_Freq, No_Freq, i, k;
Array.Sort(_M);
k = _M[0];
Max_Freq = 0; i = 0; x = 0;
while (i < _M.Length)
{
//No_Freq= the frequency of the current number
No_Freq = 0;
//X …Run Code Online (Sandbox Code Playgroud)