TITLE: If $\lvert\operatorname{Hom}(H,G_1)\rvert = \lvert\operatorname{Hom}(H,G_2)\rvert$ for any $H$ then $G_1 \cong G_2$
QUESTION [41 upvotes]: Let $G_1$ and $G_2$ be two finite groups such that for any finite group $H$, $\lvert\operatorname{Hom}(H,G_1)\rvert = \lvert\operatorname{Hom}(H,G_2)\rvert$. How can I show that $G_1 \cong G_2$ ?

REPLY [15 votes]: As mentioned in bof's answer this result is due to L. Lovász, Operations with structures. The argument is given on pages 326-327. But it doesn't use inclusion-exclusion, but rather what I could call a fibration argument. Let me present the proof in my own words.

Let us denote by $\mathrm{Hom}(-,-)$, $\mathrm{Epi}(-,-)$, $\mathrm{Mono}(-,-)$ the sets of homomorphisms, epimorphisms and monomorphisms. Up to isomorphism there is only a countable set of finite groups, say $\{G_1,G_2,G_3,\dotsc\}$ with $G_i \not\cong G_j$ for $i \neq j$.
Claim 1. If $G,H$ are finite groups, then
$$|\mathrm{Hom}(H,G)| = \sum_i \frac{|\mathrm{Epi}(H,G_i)|}{|\mathrm{Aut}(G_i)|} \cdot |\mathrm{Mono}(G_i,G)|.$$
Notice that this sum is essentially finite.
Proof. Let $\mathrm{Hom}_i(H,G) \subseteq \mathrm{Hom}(H,G)$ be the set of all homomorphisms whose image is isomorphic to $G_i$. Then $\mathrm{Hom}(H,G) = \sqcup_i \mathrm{Hom}_i(H,G)$, so that it suffices to prove that
$$|\mathrm{Hom}_i(H,G)| = \frac{|\mathrm{Epi}(H,G_i)|}{|\mathrm{Aut}(G_i)|} \cdot |\mathrm{Mono}(G_i,G)|.$$
Consider the composition map $\mathrm{Epi}(H,G_i) \times \mathrm{Mono}(G_i,G) \to \mathrm{Hom}_i(H,G)$. It is surjective, and in fact a torsor under the obvious action from $\mathrm{Aut}(G_i)$ (since image factorizations are essentially unique). The claim follows. $\square$
Let $G,G'$ be two finite groups such that $|\mathrm{Hom}(H,G)|=|\mathrm{Hom}(H,G')|$ for all finite groups $H$.
Claim 2. We have $|\mathrm{Mono}(H,G)| = |\mathrm{Mono}(H,G')|$ for all finite groups $H$.
From this it will follow immediately that there is a monomorphism $G \to G'$ and a monomorphism $G' \to G$. In particular $|G|=|G'|$ and therefore these monomorphisms are already isomorphisms $G \cong G'$.
Proof. By induction on $|H|$. We can decompose $\mathrm{Hom}(H,G)$ into the monomorphisms and the homomorphisms whose image is some $G_i$ with $|G_i| < |H|$. Hence, the first claim gives
$$|\mathrm{Hom}(H,G)| = |\mathrm{Mono}(H,G)| + \sum\limits_{ i,~ |G_i|< |H|} \frac{|\mathrm{Epi}(H,G_i)|}{|\mathrm{Aut}(G_i)|} \cdot |\mathrm{Mono}(G_i,G)|$$
Since $|\mathrm{Hom}(H,G)|=|\mathrm{Hom}(H,G')|$ and $|\mathrm{Mono}(G_i,G)|=|\mathrm{Mono}(G_i,G')|$ by induction, it follows $|\mathrm{Mono}(H,G)| = |\mathrm{Mono}(H,G')|$.
Category-theoretic generalization. Instead of the category of finite groups $\mathsf{FinGrp}$, we can work with any category $\mathcal{C}$ with the following properties:

For every $G,H \in \mathcal{C}$ the set $\mathrm{Hom}(G,H)$ is finite.
Every $G \in \mathcal{C}$ has only finitely many subobjects.
Every object $G \in \mathcal{C}$ has a size $|G| \in \mathbb{N}$ such that for every proper subobject $H < G$ we have $|H| < |G|$, likewise for proper quotients.
Every morphism in $\mathcal{C}$ factors essentially unique as an epimorphism followed by a monomorphism.

Notice that any category of finite algebraic structures of a given type has these properties (this case was also considered by L. Lovász). The same proof as above shows: If $G,G' \in \mathcal{C}$ satisfy $|\mathrm{Hom}(H,G)|=|\mathrm{Hom}(H,G')|$ for all $H \in \mathcal{C}$, then $G \cong G'$.