简体   繁体   English

在整数数组中查找前3个最大重复数

[英]To find the top 3 maximum repeated numbers in a integer array

I want to find the top 3 maximum repeated numbers in a Integer array? 我想在Integer数组中找到前3个最大重复数吗?

Below is the piece of code which I have tried but I couldn't find the desired result: 以下是我尝试过但无法找到所需结果的代码:

static void Main(string[] args)
    {

      int[,] numbers = {
                           {1, 2, 0, 6 },
                           {5, 6, 7, 0 },
                           {9, 3, 6, 2 },
                           {6, 4, 8, 1 }
                        };           
        int count = 0;
        List<int> checkedNumbers = new List<int>();
        foreach (int t in numbers)
        {
            if (!checkedNumbers.Contains(t))
            {
               foreach (int m in numbers)                   
               {
                   if (m == t)                        
                      {   
                         count++;
                      }
                }
                Console.WriteLine("Number {0} is Repeated {1} Times ", t, count);
               count = 0;
                checkedNumbers.Add(t);
            }           
       }
        Console.ReadLine();
    }

You can use GroupBy from LINQ then OrderByDescending based on count in each group: 您可以根据每个组中的计数使用LINQ中的GroupBy ,然后使用OrderByDescending

var result = list.GroupBy(i => i)
                 .OrderByDescending(g => g.Count())
                 .Select(g => g.Key)
                 .Take(3);

Edit: With your code, you can use OfType to flatten your matrix then use the code above: 编辑:使用您的代码,您可以使用OfType展平矩阵,然后使用上面的代码:

int[,] numbers = {
                       {1, 2, 0, 6 },
                       {5, 6, 7, 0 },
                       {9, 3, 6, 2 },
                       {6, 4, 8, 1 }
                 };

var list = numbers.OfType<int>();
int[] numbers = {1, 2, 3, 5, 6, 32, 2, 4, 42, 2, 4, 4, 5, 6, 3, 4};
var counts = new Dictionary<int, int>();
foreach (var number in numbers)
{
    counts[number] = counts[number] + 1;
}
var top3 = counts.OrderByDescending(x => x.Value).Select(x => x.Key).Take(3);

Hint: 暗示:

You can do this with the help of LINQ. 您可以在LINQ的帮助下执行此操作。
This is the code to find most frequest occuring element:- 这是查找出现频率最高的元素的代码:-

List<int> list = new List<int>() { 1,1,2,2,3,4,5 };

// group by value and count frequency
var query = from i in list
            group i by i into g
            select new {g.Key, Count = g.Count()};

// compute the maximum frequency
int frequency = query.Max(g => g.Count);

// find the values with that frequency
IEnumerable<int> modes = query
                              .Where(g => g.Count == frequency)
                              .Select(g => g.Key);

// dump to console
foreach(var mode in modes) {
    Console.WriteLine(mode);
}

In the same manner you can find the other two also. 同样,您也可以找到其他两个。

I see that none of the existing answers provide an explanation, so I will try to explain. 我发现现有答案均未提供任何解释,因此我将尝试进行解释。

What you need to do is to count how many times each item appears in the array. 您需要做的是计算每个项目出现在阵列中的次数。 To do that, there are various methods (dictionaries, linq etc). 为此,有多种方法(字典,linq等)。 Probably it would be easiest to use a dictionary which contains the number, and how may times it appeared: 可能最简单的方法是使用包含数字以及其出现时间的字典:

int numbers[] = {1, 3, 6, 10, 9, 3, 3, 1, 10} ;
Dictionary<int, int> dic = new Dictionary<int, int>();

Now iterate through every element in numbers, and add it to the dictionary. 现在遍历数字中的每个元素,并将其添加到字典中。 If it was already added, simply increase the count value. 如果已经添加,只需增加计数值即可。

foreach (var i in numbers)
{
    dic[i]++; // Same as dic[i] = dic[i]+1;
}

The dictionary will automatically adds a new item if it doesn't exist, so we can simply do dic[i]++; 如果不存在,字典将自动添加一个新项,因此我们可以简单地执行dic[i]++;

Next, we need to get the highest 3 values. 接下来,我们需要获得最高的3个值。 Again, there are many ways to do this, but the easiest one would be to sort it. 同样,有很多方法可以做到这一点,但是最简单的方法就是对其进行排序。

var sorted_dic = dic.OrderByDescending(x => x.Value);

Now the first 3 items in sorted_dic are going to be the 3 values you are looking for. 现在sorted_dic中的前3个项目将是您要查找的3个值。 There are various methods to get only these 3, for example using the Take method: 有多种方法只能获取这3种方法,例如使用Take方法:

var first_3 = sorted_dic.Take(3);

Now you can iterate through these 3 values, and for example print them on the screen: 现在您可以遍历这三个值,例如在屏幕上打印它们:

foreach (var i in first_3)
{
    Console.Write("{0} appeared {1} times.", i.Key, i.Value);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM