简体   繁体   English

具有最高索引的数组中的最大值

[英]Highest value from array with highest index

I need to find most common digit in array of ints, i would also like to have the highest index(number) of them, so if there is input like [11, 22, 33] then it will return 3 instead of 1. How can I achieve that in easy way?我需要在整数数组中找到最常见的数字,我也想拥有它们的最高索引(数字),所以如果有像 [11, 22, 33] 这样的输入,那么它将返回 3 而不是 1。如何我可以通过简单的方式实现吗?

static uint mostCommonDigit(uint[] n)
    {
        uint[] numbersFrequency = new uint[10];
        foreach(uint i in n)
        {
            uint a = i;
            if (a != 0)
            {
                while (a>0)
                {
                    uint d = a % 10;
                    a = a / 10;
                    numbersFrequency[d] += 1;
                } 
            }
        }
        uint max = numbersFrequency.Max();
        int index = Array.IndexOf(numbersFrequency, max);
        return (uint)index;
    }

You could convert each element of the list to a string and concatenate them.您可以将列表的每个元素转换为字符串并将它们连接起来。 Then, you could count the occurences of each character in that string.然后,您可以计算该字符串中每个字符的出现次数。 By sorting by character count and then by character value, higher characters will be sorted first if they occur with the same frequency:通过按字符数然后按字符值排序,如果较高的字符以相同的频率出现,则将首先对其进行排序:

char MostCommonDigit(int[] list)
{
    return list.Aggregate("", (i, j) => $"{i}{j}")
        .GroupBy(c => c)
        .Select(
            g => new {
                Char = g.Key,
                Count = g.Count()
        })
        .OrderByDescending(x => x.Count)
        .ThenByDescending(x => x.Char)
        .First().Char;
}

So所以

Console.WriteLine(MostCommonDigit(new [] { 11, 22, 33 }));
Console.WriteLine(MostCommonDigit(new [] { 111, 22, 33 }));

prints印刷

3
1

You can use this linq to get your position:您可以使用此 linq 来获取您的位置:

List<int> iii  = new List<int> { 11, 22, 33 };
int yyy2 = iii.IndexOf(iii.Last(y => y.ToString().GroupBy(c => c).Select(c => c.Count()).Max() == iii.Select(x => x.ToString().GroupBy(c => c).Select(c => c.Count()).Max()).Max())) + 1;

As far as "easy" way, here is another LINQ alternative :至于“简单”的方式,这是另一个 LINQ 替代方案:

static uint mostCommonDigit(uint[] n) => 
    (uint)string.Concat(n).GroupBy(c => c).Max(g => (g.Count(), g.Key - '0')).Key

string.Concat converts the array to string (for example "112233"). string.Concat将数组转换为字符串(例如“112233”)。

GroupBy groups the characters in the string by character (for example '1' => ['1', '1'], '2' => ['2', '2']). GroupBy按字符对字符串中的字符进行分组(例如 '1' => ['1', '1'], '2' => ['2', '2'])。

The Max part is similar to ordering by the number of items in each group, then by the key of each group, and then getting the last item, but it avoids the sorting. Max 部分类似于按每组中的项数排序,然后按每组的键,然后得到最后一项,但它避免了排序。 The - '0' part converts the character key to integer. - '0'部分将字符键转换为整数。

It is probably few times slower than your solution due to the overhead from LINQ, but the difference will be in milliseconds and not noticable for such small arrays.由于来自 LINQ 的开销,它可能比您的解决方案慢几倍,但差异将以毫秒为单位,对于如此小的数组并不明显。

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

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