static int maxOccuringDigit(int n) {
int tempNum = n;
if (tempNum < 0)
tempNum = -tempNum;
int[] count = new int[10];
while (tempNum != 0) {
int rem = tempNum % 10;
count[rem] = count[rem] + 1;
tempNum = tempNum / 10;
}
int maxCount = count[0];
int digit = 0;
for (int i = 1; i < count.length; i++) {
if (count[i] > maxCount) {
maxCount = count[i];
digit = i;
} else if (count[i] == maxCount)
digit = -1;
}
return digit;
}
The task is to find which digit occurs maximum times. I need help in analyzing the time complexity of this function. I think it should be O(k) where k is the total digits in given number n. Mainly I am curious about the time complexity of for loop used here. As it will always loop for 10 times, can we consider it as constant time operation?
So total O(k + 10) ~ O(k) time. Is that correct?
Also for space complexity it is using 10 extra spaces of array only, so what will be its space complexity too?