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