Use sets!
static int NumberOfDigits(int a) {
return new HashSet<char>(Math.Abs(a).ToString()).Count;
}
We make a
into a string and then turn the string into a set of characters. Since sets cannot contain duplicate values, the count of the set is the number of distinct digits.
solved How to count the number of different digits in a number? [closed]