For example, How many 4 digit numbers are there which contains not more than 2 different digits?
Answer is 576 The first (non-zero) digit of the number F (thousands digit) can be any one of nine. The second digit S which is used if there are two digits can be any one of the nine digits different from the first.
Now consider the Hundreds Tens and Units digits in the case that there are two digits used in the number. We have two possibilities F or S to fill each place - but we exclude FFF as not involving two digits, so there are 23−1=7 possible patterns with exactly two different digits and 9×9 ways of choosing the pair of digits in the first place.
Then there are nine possibilities with just one digit.
So the total you want is 9×9×7+9=576
Well, the first digit can be any of 1---9, and the second could be any of 0---9 other than the first digit.
$$a \in \{ 1,2,\cdots,9\}, \quad b \in \{0,1,\cdots,9\} - \{a\}.$$
Each of the other five digits is either $a$ or $b$.
Number of five digit numbers with exactly two distinct digits is
$$n= 9\cdot 9 \cdot 2^3 =648.$$
UPDATE
The first two, three, or four digits could be the same non-zero digit:
Thus we must add these possibilities: $$aab**,$$ $$aaab*$$ $$aaaab,$$ which can occur in $$ 9\cdot 9 \cdot (2^2 +2 + 1) \textrm{ ways}.$$
The total is $$9\cdot 9 \cdot 15 = 1215.$$