VB.NET-如何查找数组中的所有模式(最频繁出现的数字)



因此Mode是数据集中最频繁的数字。

我已经设法解决了它,但它只能获得1个模式。这是我的代码:

Dim mode = inputX.GroupBy(Function(n) n).Select(Function(g) New With {.Number = g.Key, .Quantity = g.Count}).OrderByDescending(Function(o) o.Quantity).FirstOrDefault
If mode.Quantity > 1 Then
result = mode.Number.ToString() + "    Quantity: " + mode.Quantity.ToString()
Else
result = "None."
End If

现在,尽管我输入了29 29 35 30 30,它有两个模式,但它只显示29,这是它得到的第一个模式。我想要两种或两种以上的模式。

我一直在绞尽脑汁,一直在寻找答案,但我没能做到。

我两天前刚开始学习这门语言。

引用LINQ中的一条评论:均值、中值和模式:

Dim inputX = {29, 29, 35, 30, 30}
Dim modes = From a In
(From n In inputX
Group n By n Into g = Count()
Select g, n)
Where a.g =
(From n In inputX
Group n By n Into g = Count() Select g).Max
Select a.n
Console.WriteLine(String.Join(", ", modes))

输出:

29, 30

最新更新