0
\$\begingroup\$

Write a function named maxOccurDigit that returns the digit that occur the most. If there is no such digit, it will return -1. For example maxOccurDigit(327277) would return 7 because 7 occurs three times in the number and all other digits occur less than three times.

Other examples:

maxOccurDigit(33331) returns 3

maxOccurDigit(3232, 6) returns -1

maxOccurDigit(5) returns 5

maxOccurDigit(-9895) returns 9

The function signature is maxOccurDigit(int n)

I have done it this way:

public class checknumbers{
   public static void main(String[] args)  {
   System.out.println(maxOccurDigit(32332));
  }

 public static int maxOccurDigit(int a){
    String str2 = Integer.toString(a); // converts a into String
    char[] ch2 = str2.toCharArray(); //gets str2 into an array of char
    int[] t2 = new int[ch2.length];  //defines t2 for bringing ch2 into it
    for(int i=0;i<ch2.length;i++){ //  ASCII table 
      t2[i]= (int) ch2[i]-48;   // ch2[i] is 48 units more than what we want
    }
    int count = 1, tempCount;
    int popular = t2[0];
    int temp = 0;
    for (int i = 0; i < (t2.length - 1); i++){
    temp = t2[i];
    tempCount = 0;
    for (int j = 1; j < t2.length; j++){
      if (temp == t2[j])
        tempCount++;
    }
    if (tempCount > count){
      popular = temp;
      count = tempCount;
    }
  }
    return popular;
  } 
 }

How could I return -1 if there are two integers but methods have only one?

\$\endgroup\$
4
  • 2
    \$\begingroup\$ In Java, you can't return -1 for none, N when N is the single most occuring number, and [L, M, N] when L, M, and N are equally the three most occuring numbers (because those would be differing return types). The specification is flawed, and no code will solve this. Is the specification changeable? As a workaround you could output "75" when those two digits are most ocurring but this is getting ugly. \$\endgroup\$
    – MrBrushy
    Commented Jul 17, 2017 at 9:51
  • \$\begingroup\$ @SylvainBoisse It was a question that was asked in test exam. We can't change the main method also the function signature. I was also thinking how could integer hold 2 different int values. May be question needs a correction. But I am hoping that there may be some trick to compile the program in which one integer value holds 2 different integers :P \$\endgroup\$
    – Sus Hill
    Commented Jul 17, 2017 at 11:15
  • \$\begingroup\$ How would maxOccurDigit(3232, 6) even compile, with two parameters? I think that the test question is messed up. \$\endgroup\$ Commented Jul 17, 2017 at 11:54
  • \$\begingroup\$ Or maybe the test question was assuming there would never be two digits in the answer \$\endgroup\$
    – MrBrushy
    Commented Jul 17, 2017 at 12:34

1 Answer 1

1
\$\begingroup\$

You are passed an integer parameter, and you have to return an integer. I do not see it as worthwhile converting to a string for the counting. Better to count the digits directly from the original integer. MOD (%) 10 will give you the last integer, and DIV (/) 10 will shift the remainder along one position.

You may have to be careful with 0 and negative numbers with this method, otherwise you might get unexpected results.

As will as finding the most common digit you also have to find if there are any other digits with the same frequency. As you look for a new higher maximum, look for other digits with the same count and keep a note if they exist.

This is my code:

public static int maxOccurDigit(int a) {

    // Zero is a special case.
    if (a == 0) {
        return 0;
    }

    // Numbers != 0.
    int[] digitCounts = new int[10];

    // Ensure a positive
    if (a < 0) {
        a *= -1;
    }

    while (a > 0) {
        digitCounts[a % 10] += 1;
        a /= 10;
    }

    // Find max count and occurrences.
    int maxCount = digitCounts[0];
    boolean sameCounts = false;
    int digit = 0;
    for (int i = 1; i < digitCounts.length; i++) {
        if (digitCounts[i] > maxCount) {
            maxCount = digitCounts[i];
            sameCounts = false;
            digit = i;
        } else if (digitCounts[i] == maxCount) {
            // Digits with equal counts.
            sameCounts = true;
        }
    }

    // Check for multiple max counts.
    if (sameCounts) {
        return -1;
    }

    // Valid single max digit
    return digit;

} // end maxOccurDigit()
\$\endgroup\$
1
  • \$\begingroup\$ You don't need the sameCounts variable, setting digitto -1in the else-block is sufficient. Additionally your implementation will fail for 1<<-1 (returns -1 instead of 4) as 1<<-1==-1<<-1. \$\endgroup\$
    – Nevay
    Commented Jul 17, 2017 at 13:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.