【问题标题】:Array - find the number that is repeated most frequently [duplicate]数组 - 找到最常重复的数字[重复]
【发布时间】:2021-06-24 06:15:45
【问题描述】:

我想知道一个数组中出现最多的数字。

例子:

8 4 3 8 4 4 1 5

在这种情况下,我希望程序告诉我数字是 4。

这是我到目前为止所写的,但 console.Writeline 返回错误 system.32。

你能帮忙吗?

int[] moda = new int[21];

for (int j = 0; j < avaliacoes.Length; j++)
{
    int avaliacao = avaliacoes[j];

    moda[avaliacao] = moda[avaliacao] + 1;
}

Console.WriteLine("\nA moda é{0}: ", moda);

【问题讨论】:

    标签: c#


    【解决方案1】:

    按组大小排序应该可以完成工作。

    总结一下我在做什么,我按那里的值对数组进行分组,然后我将该组发送到一个匿名类型中,该类型将保存密钥以及组计数。第三,我按降序排序,因此我们从高值到低值排序或可枚举,这将允许我说枚举中的第一个元素必须是最高值(如果不等于最高值)。

    class Program
    {
        static void Main(string[] args)
        {
            var a = new[] { 8, 4, 3, 8, 4, 4, 1, 5 };
            var mostPresent = a
                .GroupBy(e => e)
                .Select(e => new { Key = e.Key, Count = e.Count() })
                .OrderByDescending(e => e.Count)
                .FirstOrDefault();
            Console.WriteLine(mostPresent);
        }
    }
    

    【讨论】:

      【解决方案2】:

      有很多方法可以做到这一点,这里是一种:

      static void Main(string[] args)
          {
              // Create array
              const int arrLength = 10;
              int[] mainArray = new int[arrLength];
      
              // Fill array with random numbers
              Random randNum = new Random();
              for (int i = 0; i < mainArray.Length; i++)
              {
                  mainArray[i] = randNum.Next(10);
              }
      
      
              // Create a dictionary for counting
              Dictionary<int, int> countDic = new Dictionary<int, int>();
      
              foreach (var currentNumber in mainArray)
              {
                  // If the current number has appeared before increase the count for that number
                  if (countDic.TryGetValue(currentNumber, out int _))
                  {
                      countDic[currentNumber]++;
                  }
      
                  // If it's first time current number has appeared set its count as one
                  else
                  {
                      countDic.Add(currentNumber, 1);
                  }
              }
      
              // Print frequency of numbers
              foreach (var num in countDic)
              {
                  Console.WriteLine($"{num.Key} appears {num.Value} times in the array!");
              }
      
              // Print the number which appears the most in the array
              int maxNum = countDic.Aggregate((x, y) => x.Value > y.Value ? x : y).Key;
              Console.WriteLine(maxNum);
      
              Console.Read();
          }
      

      解决方案中有什么不明白的地方问我

      【讨论】:

        【解决方案3】:

        您可以使用 LINQ 轻松做到这一点。我将在下面解释发生了什么:

        using System;
        using System.Collections.Generic;
        using System.Linq;
        
        namespace ConsoleApp1
        {
            class Program
            {
                static void Main(string[] args)
                {
                    List<int> ints = new List<int> { 8, 4, 3, 8, 4, 4, 1, 5 };
        
        
                    var query = from i1 in ints
                                select new
                                {
                                    number = i1,
                                    count = ints.Where(i2 => i2 == i1).Count()
                                };
        
                    var most = query.OrderByDescending(x => x.count).First().number;
        
        
                    Console.WriteLine($"Number: {most}");
                    Console.ReadKey();
                }
            }
        }
        
        • 在第一个查询中,我遍历列表中的所有整数并创建一个具有 2 个属性“数字”和“计数”的匿名对象。该计数包含列表中整数的数量以及第一次迭代的次数。

        • 在第二行中,“最多”是通过在“计数”上按降序排列匿名类型并取该结果的第一项来选择的。如果您的数组中有更多具有相同条目数的数字,您将只得到第一个。因此,如果“4”和“5”都出现 4 次,则不能保证您会得到哪个结果。它可以是“4”,但也可以是“5”。

        【讨论】:

          猜你喜欢
          • 2017-05-30
          • 2020-04-03
          • 1970-01-01
          • 2015-02-15
          • 2019-02-02
          • 1970-01-01
          • 2021-06-14
          • 1970-01-01
          • 2016-08-06
          相关资源
          最近更新 更多