The question says: “ ...at least one element occurs exactly twice.”
Case 1: one element repeat twice and the other two elements are distinct, as in {1,1,2,3}
Here we choose one element from n elements for repetition
and rest two distinct elements from the remaining (n-1) elements
Hence the number of ways: $\binom{n}{1}.\binom{n-1}{2}$
Case 2: two elements repeat twice, as in {1,1,2,2}
Here we choose two distinct elements from n elements for repetition
Hence the number of ways : $\binom{n}{2}$
By the fundamental principle of counting, the required answer is $\binom{n}{1}.\binom{n-1}{2} +\binom{n}{2}$