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