How many multisets of size $4$ that can be constructed from $n$ distinct elements so that at least one element occurs exactly twice ?
Example : For $n=3$ and element set as $\{1,2,3\}$ I am getting multisets as: $\{1,1,3,3\}, \{1,1,2,2\}, \{1,1,2,3\}, \{2,2,3,3\}, \{2,2,1,3\}, \{3,3,1,2\}$, which are total $6$.
I am looking for a formula for large N. Is there any such formula or do we have to count manually?
Represent a multiset
$$\{\underbrace{1,\dots,1}_{x_1\text{ times}},\underbrace{2,\dots,2}_{x_2\text{ times}},\dots,\underbrace{n,\dots,n}_{x_n\text{ times}}\}$$
by $\{1^{x_1},2^{x_2},\dots,n^{x_n}\}$. For each $j=1,\dots,n$ a mulsitet of size $4$ in which $x_j$ occurs exactly twice corresponds to a solution of
$$ \left\{\begin{array}{l} \displaystyle\sum_{\substack{1\leq i\leq n}\\i\neq j} x_i= 2\\ x_i\geq 0 \end{array}\right.$$
The number of solutions to this system can be found via stars and bars, and that number is $\binom{2+(n-1)-1}{2}=\binom{n}{2}$. This might point in the direction that the answer is $n\binom{n}{2}$, but we're overcounting.
Indeed, when $j_1\neq j_2$, the multiset $\{{j_1}^2,{j_2}^2\}$ is counted twice: once for the index $j_1$, and once for the index $j_2$. It's easy to see this is the only case of overcounting. How many such sets there are?
That's just the number of ways to choose two distinct indices from among $\{1,\dots,n\}$, that is, $\binom{n}{2}$. Hence, the final answer is