function countDigits(n) {
n = Math.abs(n);
if (n === 0) return 1;
let count = 0;
while (n > 0) {
count++;
n = Math.floor(n / 10);
}
return count;
}
Explanation: We repeatedly divide the number by 10 until it becomes zero, counting each step as one digit.
β± O(log n) | πΎ O(1)