I have to find all group homomorphisms from quaternion group $Q_8=\{\pm 1, \pm i, \pm j,\pm k\}$ to itself . I tried it as
Since $Aut(Q_8)\cong S_4$ so there are $24$ automorphisms which are also homomorphisms. One is trivial homomorphism which maps all elements to identity and there are $4$ homomorphisms whose range are inside $\{\pm 1\}$ as they are factor through $\frac{Q_8}{\{\pm 1\}}\cong \mathbb Z_2\times\mathbb Z_2.$ Now there is no onto maps from $Q_8$ onto $\{\pm 1,\pm i\}$, $\{\pm 1,\pm j\}$, $\{\pm 1,\pm k\}$ as these are cyclic groups of order $ 4$ but $\frac{Q_8}{\{\pm 1\}}\cong \mathbb Z_2\times\mathbb Z_2$ has no element of order $4$. So I conclude that in total there are $24+1( trivial)+3=28$ group homomorphisms.
Please comment if any mistakes. Thank you.
See Rajkumar for the number of homomorphisms $f\colon Q_{4m}\rightarrow Q_{4n}$ for the generalized quaternion groups. Theorem 2.4. shows that the number, for $m$ and $n$ even with $m\equiv 2\bmod 4$ is given by $$ 4+8n+2n\left( \sum_{k\mid (2m,2n),k\nmid m} \phi(k)\right). $$
For $m=n=2$ this gives $28$ homomorphisms, as you have shown.