-5

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?

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;
    }
7
  • 1
    " 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"? I don't see how you get 3 from this? The indices of [11, 22, 33] are 0, 1, and 2. Can you clarify what you're after a little? Are you saying you end up with 3 because there's a tie between the number of 1s, 2s, and 3s and 3 is the highest? Using the word "index" might be throwing me off. Are you ending up with 3 because 33 is the last index? If it was [33,22,11] you'd end up with 1? More examples might help.
    – itsme86
    Commented May 9, 2019 at 21:50
  • I mean if i get input [11,22,33] then function will count how many occurences of each digit are in the array. So the numbersFrequency[1]=2;numbersFrequency[2]=2;numbersFrequency[3]=2 and then I want to get a highest value and also highest index, so the function won't return 1, but 3. It will return 1 both in cases of [11,22,33] and in case of [33,22,11]
    – user9335104
    Commented May 9, 2019 at 21:54
  • Indices in C# are zero-based. They don't start at 1 and go up; they start at 0 and go up.
    – itsme86
    Commented May 9, 2019 at 21:55
  • I know that's why for d=0 numbersFrequency[0]+=1; so it will count any occurence of 0 and so on numbersFrequency[1] means number of occurences of 1 etc.
    – user9335104
    Commented May 9, 2019 at 22:13
  • is it the max digit of the most common digits ? for example 2 in [11, 22, 3] ?
    – Slai
    Commented May 9, 2019 at 22:18

3 Answers 3

0

You can use this linq to get your position:

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;
0

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
2
  • That's not i've wanted, but thank you too, it looks nice. It will be faster than my solution?
    – user9335104
    Commented May 9, 2019 at 22:29
  • Can you please describe how this is not what you're looking for? I'm having a hard time understanding the question, and I thought this was it. In the first case, the most repeated digit count is 2, and the highest digit that has that count is 3. In the second case, the most repeated digit count is 3, and the highest number that has that many digits repeated is 1. Perhaps updating your question with more sample input and expected output would help clarify your requirements. There is confusion around indexes, digits, numbers, and counts.
    – Rufus L
    Commented May 9, 2019 at 23:19
0

As far as "easy" way, here is another LINQ alternative :

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").

GroupBy groups the characters in the string by character (for example '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. The - '0' part converts the character key to integer.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.