Say I'm trying to find the number of onto(surjective) functions from the set $A=(1,2,3,...n)$ to the set $B=(1,2,3)$.
The total number of functions from A to B is $3^n$ as each element in a has three options to be mapped to.
Then I subtracted all possibilities where one of the elements of B is not mapped to, $3*2^n$ in number, as there are three choices of the skipped element of B and two each for all elements in A. But this double counts all cases where only one element of B is mapped to, of which there are three, so adding them gives me.