Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.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/8/linq/3.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# 使用LINQ选择数组中最小、最常见的数字_C#_Linq - Fatal编程技术网

C# 使用LINQ选择数组中最小、最常见的数字

C# 使用LINQ选择数组中最小、最常见的数字,c#,linq,C#,Linq,如果我有一个未排序的数组,它有多对相同的数字,我如何才能找到最常见的数字中最小的一个 int[] arr = new int[] {8, 6, 5, 2, 5, 9, 6, 9, 2, 3}; // unsorted array // Array.Sort(arr); // I could sort the array int mostCommon = arr.GroupBy(ii => ii) //Grouping same items .OrderByDe

如果我有一个未排序的数组,它有多对相同的数字,我如何才能找到最常见的数字中最小的一个

int[] arr = new int[] {8, 6, 5, 2, 5, 9, 6, 9, 2, 3}; // unsorted array
// Array.Sort(arr); // I could sort the array

int mostCommon =  arr.GroupBy(ii => ii)  //Grouping same items
            .OrderByDescending(abc => abc.Count()) //now getting frequency of a value
            .Select(bcd => bcd.Key) //selecting key of the group
            .FirstOrDefault();   //Finally, taking the most frequent value
在上面的例子中,我想得到2,但查询返回6。如果在运行查询之前对数组进行排序,我将得到2,但我想知道是否有方法使用LINQ从未排序的数组中获取最小的最常见值。
我对代码的快速执行不感兴趣。

有两个2、两个6和两个9-假设您的意思是在出现平局时,最低(2)应该优先,那么您只需要添加第二个顺序,如下所示:

int mostCommon = arr.GroupBy(x => x)
            .OrderByDescending(grp => grp.Count()) // First precedence = frequency
            .ThenBy(grp => grp.Key) // Second precedence is lowest number first
            .Select(bcd => bcd.Key)
            .FirstOrDefault();
编辑、重做(N)解决方案

这里有一种方法,求助于命令式方法,它可以在一次数据传递中完成。假设您在数组中指定了个位数,我假设一个仓位计数数组的范围为0-10(好处是这些值被初始化为零),但如果范围更大,显然会进行调整。如果您的值较大且可能稀疏,则可能需要用字典替换数组

var bins = new int[10]; // Adjust this to size / use Dictionary if sparse
var hiCount = 0;
var smallestMostCommon = int.MaxValue;
foreach(var a in arr)
{
   var newCount = ++bins[a];
   if (newCount > hiCount) // 1st Precedence : Frequency
   {
      hiCount = newCount;
      smallestMostCommon = a;
   }
   else if (newCount == hiCount && a < smallestMostCommon) // 2nd : Lowest preferred
   {
      smallestMostCommon = a;
   }
}
var-bins=new int[10];//调整此大小/如果稀疏,则使用字典
var hiCount=0;
var smallestMostCommon=int.MaxValue;
foreach(arr中的var a)
{
var newCount=++bin[a];
if(newCount>hiCount)//第一优先级:频率
{
hiCount=新计数;
最小最常见=a;
}
else if(newCount==hiCount&&a

我确信,进一步的优化是可能的,特别是在循环中的任何一点上,如果剩余的元素数量小于第一个和第二个最高存储箱之间的差异,那么循环可以提前终止。

在选择
之前,您需要再次订购项目,因此
FirstOrDefault
将返回最小的组键:

int smallestMostCommon =  arr.GroupBy(ii => ii)  //Grouping same items
        .OrderByDescending(abc => abc.Count()) //now getting frequency of a value
        .ThenBy(g => g.Key) // Make sure we get the smallest key first
        .Select(bcd => bcd.Key) //selecting key of the group
        .FirstOrDefault();   //Finally, taking the most frequent value

使用排序算法按数字顺序对数组中的值进行排序。搜索“气泡排序”。我喜欢循环这个算法以确保准确性。然后对产品执行相同的排序以确定相似的值,并打印结果

非常感谢,这就是我要找的!有趣的是,我们刚刚就Meta()进行了长时间的讨论,
OrderBy
对于“单一最常见…”来说是错误的理论解决方案,OP不应该接受这样的解决方案,而应该要求更好的线性时间解决方案。。。(在我看来,除性能至关重要的情况外,此处显示的排序在大多数情况下都可以,但无论如何都可能不会使用than LINQ:))Alexei,请提供更快的解决方案,我将接受您的答案。就我而言,我很高兴有一个有效的解决方案,但为了所有遇到这个问题的人的利益,最好接受最有效的解决方案。@Nick_F-我给了O(N)一个建议非常感谢你,Stuart。旁注:你可能想在帖子中加一句话,你需要的只是正确的代码,不要在意理论性能,因为到目前为止,答案只提供了(显然,如果你需要性能,你会寻找O(n)解决方案,而不是O(n log n))你是对的,mjwills,我的代码是错误的。我最初尝试使用ThenBy,但不知道如何构建查询。您能为最大最常见的号码显示正确的代码吗?谢谢。对那些会思考的人来说,这很容易;)