Surjection iff Epimorphism in Category of Sets

Theorem
Let $\mathbf{Set}$ be the category of sets.

Let $f: X \to Y$ be a morphism in $\mathbf{Set}$, i.e. a mapping.

Then $f$ is a surjection iff it is an epimorphism.

Necessary Condition
Suppose that $f$ is surjective.

Suppose further that we have mappings $g, h: Y \to Z$ such that $g \ne h$.

Then necessarily there exists some $y \in Y$ such that $g \left({y}\right) \ne h \left({y}\right)$ by Equality of Mappings.

As $f$ is surjective, it follows that there is an $x \in X$ such that $f \left({x}\right) = y$.

Hence, we conclude that:


 * $g \left({f \left({x}\right)}\right) \ne h \left({f \left({x}\right)}\right)$

which, again by Equality of Mappings, means that $g \circ f \ne h \circ f$.

Therefore, $f$ is epic, by the Rule of Transposition.

Sufficient Condition
Suppose that $f: X \twoheadrightarrow Y$ is an epimorphism.

By definition of surjection, it will suffice to show that:


 * $\forall y \in Y: \exists x \in X: f \left({x}\right) = y$

Let us reason by contradiction.

So suppose $f$ were not surjective.

Then there would be an $y_0 \in Y$ such that:


 * $\forall x \in X: f \left({x}\right) \ne y_0$

Consider the mappings defined by:


 * $g: Y \to Y \cup \left\{{Y}\right\}, g \left({y}\right) := y$
 * $h: Y \to Y \cup \left\{{Y}\right\}, h \left({y}\right) := \begin{cases}y & \text{if $y \ne y_0$}\\

Y & \text{if $y = y_0$} \end{cases}$

The assumption on $f$ yields that $g \circ f = h \circ f$.

Since $h \ne g$, it follows that $f$ cannot be epic.

This contradiction shows that $f$ is necessarily surjective.