Number of 4-digit numerals with at most 2 different digits

André has come up with a nice and simple alternative solution, but I'm going to try to salvage your method, which is essentially correct.

  1. This step does indeed give you 9 numbers.
  2. There are 9, not 8, numbers between 1 and 9, so you want to use $9 \choose 2$ not $8 \choose 2$. Indeed, $${9 \choose 2} \times \left(\frac{4!}{2!\times2!} + 2\times\frac{4!}{3!\times1!}\right) = 504$$
  3. You've counted too many here, because you've not excluded putting the zero first. There are actually only three ways of having one zero (8880, 8808, 8088) and three of having two (8800, 8080, 8008). Hence the total number of ways, including the one way of having three zeroes, is $9 \times (3 + 3 + 1) = 63$.

Now $9 + 504 + 63 = 576$. So you had the right approach, but you just messed up some of the arithmetic.


We have the $9$ single-digit numbers.

For the others, the first digit can be chosen in $9$ ways. For each of these choices, the "other" digit can be chosen in $9$ ways. For each of these choices, we can have the other digit in $1$, $2$, or $3$ places. These places can be chosen in $\binom{3}{1}+\binom{3}{2}+\binom{3}{3}$ ways, for a total of $7$ (alternately, this can be viewed as $2^3-1$, all but choosing $0$ places for the other digit). This gives a count of $(9)(9)(7)$, which is $567$.

Now add the $9$.

Note that precisely the same procedure works for, say, $17$-digit numbers. Nothing changes, except that the $2^3-1$ is replaced by $2^{16}-1$.