Proof of Principle of Inclusion and Exclusion [duplicate]

This discussion/explanation (Art of Problem Solving, 2021) of the Principle of Inclusion and Exclusion seems to show (at least to me) that for sets $\textit{A}$ and $\textit{B}$,

$|\textit{A} \cup \textit{B}| = |\textit{A}| + |\textit{B}| - |\textit{A} \cap \textit{B}|$

using an intuitive approach.

Can someone help and give a formal proof outline (or complete proof) to prove such claim?

Regards.


Note that the page you linked gave a general proof, but perhaps it isn't clear since they have a union of $n$ sets. So let us just focus on the case $n=2$. You wish to show that $$\vert A\cup B\vert=\vert A\vert+\vert B\vert -\vert A\cap B\vert$$ Thus, let $x\in A\cup B$, we wish to show that $x$ is counted only once on the right-hand side of the equation. We have three cases:

Case 1: $x\in A$ and $x\notin B$, in this case we have that $x$ contributes a value of $1$ to $\vert A \vert$, and does not contribute anything to $\vert B\vert$ nor $\vert A\cap B\vert$, so $x$ is only counted a single time.

Case 2: $x\in B$ and $x\notin A$ this is done analogously to case 1.

Case 3: $x\in A$ and $x\in B$, then $x$ contributes a value of $1$ to $\vert A\vert$, $\vert B\vert$, and $\vert A\cap B\vert$, and as we are subtracting $\vert A\cap B\vert$, we cancel out the fact that we double-counted $x$.