'How to find sequence of identical numbers using recursion? -- Java

I have an assignment where i get an number input from the user, for example : "57779227" and i need to return the longest sequence of identical numbers. For this example, the longest sequence is "777" and the return should be 3 (as the amount of times the number "7" is in a row.

So far I wrote an iteration method. ***No loops to be used in this method, ONLY RECURSION. ***

Iteration example :

public static int maxSequence(int num) {
        int max = 1;                        //initiate
        int currentCount = 1;
        int prevDigit = 11;//Because num%10 != 11 Always!
        int currentDigit;
        
        while (num!=0) {
            currentDigit = num%10;
            if (prevDigit == currentDigit)
                currentCount++;
            else if (currentCount > max)
                max = currentCount;
            
            if (prevDigit != currentDigit) //initiate for the next Iteration
                currentCount = 1;
            prevDigit = currentDigit;
            num = num/10;
        }
        return max;
    }


Solution 1:[1]

When previousDigit != currentDigit then a new count will be start

public static int maxSequence(int num) {
        int previousMax = 1;
        int currentMax = 1;
        int previousDigit = num % 10;
        num /= 10;
        
        while (num != 0) {
            int currentDigit = num % 10;
            if (previousDigit == currentDigit) {
                currentMax++;
            } else {
                if (previousMax < currentMax) {
                    previousMax = currentMax;
                }
                currentMax = 1;
                previousDigit = currentDigit;
            }
            num /= 10;
        }
        
        return Math.max(currentMax, previousMax);
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Frank huaylinos velasquez