I'd like to prove the following theorem :
Let $G$, $H$, $G'$, $H'$ four groups. If they are finite then $$ \left\lbrace \begin{array}{ll} G\times H \simeq G'\times H' \\ G\simeq G' \end{array}\right. \quad \Rightarrow\quad H\simeq H'.$$
This not stands with infinite groups. Consider for example $G=\mathbb{Z}[X]$, $G'=\bigoplus_{n\geq 0}\mathbb{Z}$ and $H=\mathbb{Z}$, $H'=\lbrace \mathrm{Id} \rbrace$.
Our teacher proved the theorem using the following method : we first show that for all finite groupe $L$ the number of morphism beetwen $L$ and $H$ is the same that beetween $L$ and $H'$. Then we proved that the number of one to one morphism beetwen $H$ and $H$ is the same that beetwen $H$ and $H'$. We concluded by cardinality.
I'm okay with this proof but I find it a bit artificial. I've tried to find this theorem in books but after hours of rechearch I wasn't able to find any book which prove this theorem.
If you see another proof, or if you know where I can find it, please let me know. Thank you for your help.