Search code examples
javaarraysdigits

Output of finding maxFreq of digit in an array of numbers does not come as expected


Given an array of numbers as input, return the digit which occurs the maximum number of times in the input.

MyApproach

I first separated each digit of each member of the array. Then, I counted the frequency of each digit and then I found the max number of times the digit occurs and noted its position. And when I searched for the position in the array of separation of digits I found the digit that occurs maximum number of times.

This is what I tried to do with the following code:

 int[] seperateDigits(int[] numbers)
 {

  int c[]=new int[numbers.length*2];
  for(int i=0;i<numbers.length;i++)
   {
     for(int k=0;numbers[i]>0;i++)
     {

           int q=numbers[i]%10;   //used this logic for separation of digits
           System.out.println(numbers[i]);

           c[k]=q;
           System.out.println(c[k]);
            k++;
           numbers[i]=numbers[i]/10;

     }
    }
       return c;
  }

    int countMaxFreq(int c[])
    {
        int t[]=new int[c.length];
        int count=1;
       if(c.length<2)
       return count;
       else
       {

           //used the logic for finding maximum frequency of each digit.
           int m=0;
          for(int i=0;i<c.length;i++)
          {
              for(int j=i+1;j<c.length;j++)
              {
                  if(c[j]==c[i])
                 {
                    count++;

                 }

              }  
               t[m++]=count;

          }
       }  
       if(c.length<2)
       return c[0];
       else
       {

        int max=t[0];
        int max_index=0;
        for(int i=1;i<t.length;i++)
         {
           if(t[i]>=max)    //used the logic for finding frequency.
           {
               max_index=i;
           }
         }
         for(int l=0;l<c.length;l++)
         {                               //Return the position of the frequent element.
             if(l==max_index)
             {
               break;
             }
         }
         return max_index;
       }
    }
} 

Solution

  • You can simplify the approach, to this algorithm:

    • create a int[] counts = new int[10], there are at most 10 digits
    • for each number in the input
      • for each digit
      • increment the count of the digit
    • find the max element, return its index, that's the digit you're looking for

    For example:

    if (numbers.length == 0) {
        throw new NoSuchElementException("no numbers, so 'most frequent' makes no sense");
    }
    
    int[] counts = new int[10];
    
    for (int num : numbers) {
        if (num == 0) {
            ++counts[0];
        } else {
            num = Math.abs(num);
            while (num > 0) {
                int digit = num % 10;
                num /= 10;
                ++counts[digit];
            }
        }
    }
    
    return IntStream.range(0, counts.length)
        .reduce((i, j) -> counts[i] < counts[j] ? j : i)
        .getAsInt();
    

    Note: when there are multiple digits with the same count, this implementation will return the smaller digit.