Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 仅使用一次传递查找数组中最频繁的数字_C#_Arrays - Fatal编程技术网

C# 仅使用一次传递查找数组中最频繁的数字

C# 仅使用一次传递查找数组中最频繁的数字,c#,arrays,C#,Arrays,因此,我试图找到一种算法,只需一次遍历就可以搜索数组中出现频率最高的数。我曾尝试使用两个内部循环来解决它,但这需要在循环中多次遍历数组 现在,我正试图找到另一种方法,只需一次“扫描”数组,找到最频繁的数字。有人有什么建议会有帮助吗 使用LINQ,您只需使用 var mostfrequent = myList.GroupBy(i=>i).OrderByDescending(grp=>grp.Count()) .Select(grp=>grp.Key).First();

因此,我试图找到一种算法,只需一次遍历就可以搜索数组中出现频率最高的数。我曾尝试使用两个内部循环来解决它,但这需要在循环中多次遍历数组


现在,我正试图找到另一种方法,只需一次“扫描”数组,找到最频繁的数字。有人有什么建议会有帮助吗

使用LINQ,您只需使用

var mostfrequent = myList.GroupBy(i=>i).OrderByDescending(grp=>grp.Count())
      .Select(grp=>grp.Key).First();

编辑

基于OP使用哈希表的需求,它可以实现如下:

            int mostCommom = array[0];
            int occurences = 0;
            foreach (int num in array)
            {
                if (!hs.ContainsKey(num))
                {
                    hs.Add(num, 1);
                }
                else
                {
                    int tempOccurences = (int)hs[num];
                    tempOccurences++;
                    hs.Remove(num);
                    hs.Add(num, tempOccurences);

                    if (occurences < tempOccurences)
                    {
                        occurences = tempOccurences;
                        mostCommom = num;
                    }
                }
            }
            foreach (DictionaryEntry entry in hs)
            {
                Console.WriteLine("{0}, {1}", entry.Key, entry.Value);
            }
            Console.WriteLine("The commmon numer is " + mostCommom + " And it appears " + occurences + " times");
int-mostcomom=array[0];
int发生率=0;
foreach(数组中的int num)
{
如果(!hs.ContainsKey(num))
{
hs.添加(数量,1);
}
其他的
{
int tempOccurrences=(int)hs[num];
临时事件++;
hs.删除(num);
hs.Add(num,tempocurrences);
如果(发生次数<临时发生次数)
{
事件=临时事件;
mostCommom=num;
}
}
}
foreach(字典输入hs)
{
WriteLine(“{0},{1}”,entry.Key,entry.Value);
}
Console.WriteLine(“commmon编号为“+mostcomom+”,显示为“+occurrencess+”times”);

您可以使用字典并将键设置为数字,然后当您遇到一个已经遇到的数字时,在当前数字(即键)的值上添加1,当数组完成时,选择具有最高值的键。

类似的方法应该可以工作(您需要获取ValueTuple NuGet包才能使该元组调用签名生效):

而且,如果您想要所有可能的结果,请在返回之前执行以下操作:

var maxes = from pair in counters where pair.Value == maxNumOccurances select pair;

那么在C#或Java中呢?@TomCold是在C#中!@TomCold这是在C中?有人能解释一下发生了什么吗?我也在考虑使用hashtable,但这也违反了我想要的规则。这违反了规则吗?还是使用hashtable更有效?不,你只需要在我认为Sajeetharan对第一个a的评论时循环数组就可以了nswer实际上就是我刚才所说的,但在代码版本中^^^是的,但我试图知道使用字典或哈希表的差异。LINQ特定的副本:,
if (occurances > maxNumOccurances)
var maxes = from pair in counters where pair.Value == maxNumOccurances select pair;