C# - How to find the most common and the least common integers in an array?

拟墨画扇 提交于 2019-12-12 03:40:00

问题


I was asked to make a Dice program with two arrays (one for each dice) and add the two results, e.g.: 2 (dice 1) + 6 (dice 2) = 8.

The program must roll the dices 100 times and show the sum each time. I could do it so far, but the program also must show which sum is the most frequent, and which sum is the least frequent.

Like this: sum = [2, 2, 2, 2, 3, 3, 4, 4, 5, 6, 6]. Most common: 2; Least common: 5.

How can I do it?

This is how my code looks like:

static void Main(string[] args)
    {
        Random gerador = new Random();
        int[] soma = new int[100];
        int rolagem = 0;

        for(int i = 0; i < soma.Length; i++)
        {
            rolagem = 0;
            rolagem += gerador.Next(6) + 1;
            rolagem += gerador.Next(6) + 1;
            soma[i] = rolagem;
        }

        var mais = soma.GroupBy(item => item).OrderByDescending(g => g.Count()).Select(g => g.Key).First();
        //NEED TO FIND OUT LEAST COMMON SUM

        for (int j = 1; j < soma.Length; j++)
        {
            Console.Write("{0} ", soma[j]);
        }
        Console.WriteLine("Soma mais frequente: {0}, Soma menos frequente: {1}", mais, menos);
        Console.ReadKey();
    }

回答1:


You're almost there, you can find the least common one similarly:

var array = new[] { 1, 1, 1, 1, 4, 2, 2, 3, 3, 3, 5, 5 };
var result = array.GroupBy(i => i).OrderBy(g => g.Count()).Select(g => g.Key).ToList();

var mostCommon = result.Last();
var leastCommon = result.First();



回答2:


If you have code that rolls the dice 100 times, you are pretty close. All you need to do is frequency counters.

A roll of a pair of dice yields a number between 2 and 12, inclusive. Make an int count[13] array before entering the loop.

In the loop each time you have two numbers, say, d1 and d2, increment the count as follows:

count[d1+d2]++;

Once the loop is over, find the highest and the lowest numbers in the array between indexes 2 and 12, inclusive. The index of the highest number will be the number with the highest roll count; the index of the lowest number will be the number with the lowest roll count.




回答3:


Tuple<int, int> least = new Tuple<int, int>(-1, -1), most = new Tuple<int, int>(-1, -1);
List<int> arr = new List<int> { 2, 2, 2, 2, 3, 3, 4, 4, 5, 6, 6 };
var grp = arr.GroupBy(x => x).Select(x=>x).ToList();

foreach (var item in grp)
{
    if (least.Item2 == -1 || least.Item2>item.Count())
    {
        var x = new Tuple<int, int>(item.Key, item.Count());
        least = x;
    }

    if (most.Item2 == -1 || most.Item2 < item.Count())
    {
        var x = new Tuple<int, int>(item.Key, item.Count());
        most = x;
    }
}

Console.WriteLine("Least : "+least.Item1+" repeated " + least.Item2+"times");
Console.WriteLine("Most : "+most.Item1 + " repeated " + most.Item2 + "times");

Or as m1kael suggested,

Tuple<int, int> least = new Tuple<int, int>(-1, -1), most = new Tuple<int, int>(-1, -1);
List<int> arr = new List<int> { 2, 2, 2, 2, 3, 3, 4, 4, 5, 6, 6 };
var grp = arr.GroupBy(x => x).OrderBy(x=>x.Count()).Select(x => x.Key).ToList();
Console.WriteLine("Least : "+ grp.First());
Console.WriteLine("Most : "+ grp.Last());



回答4:


There is a small chance for more than one most or least common:

var a = Enumerable.Repeat(new Random(), 100).Select(r => r.Next(6) + r.Next(6) + 2);

var groups = a.GroupBy(i => i).GroupBy(g => g.Count(), g => g.Key).OrderBy(g => g.Key).ToList();

var mostCommon = string.Join(", ", groups.Last());
var leastCommon = string.Join(", ", groups[0]);


来源:https://stackoverflow.com/questions/43403243/c-sharp-how-to-find-the-most-common-and-the-least-common-integers-in-an-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!