As ineff said, your solution is fine, and ineff has given you the other natural solution. Since you’re studying on your own, I just want to emphasize that if $G$ is a set, equivalence relations on $G$, partitions of $G$, and functions with domain $G$ are three ways of looking at essentially the same thing.
You’ve already seen that if $R$ is an equivalence relation on $G$, $\mathscr{P}(R)=\{[x]_R:x\in G\}$ is a partition of $G$ (where $[x]_R$ is the $R$-equivalence class of $x$), and if $\mathscr{Q}$ is a partition of $G$, then the relation $E(\mathscr{Q})$ defined by $$x\;E(\mathscr{Q})\;y \iff \exists Q\in\mathscr{Q} \big(x,y\in Q\big)$$ is an equivalence relation on $G$. Moreover, $E\big(\mathscr{P}(R)\big)=R$ for any equivalence relation $R$ on $G$, and $\mathscr{P}\big(E(\mathscr{Q})\big)=\mathscr{Q}$ for any partition $\mathscr{Q}$ of $G$. In other words, there’s a natural bijection between equivalence relations on $G$ and partitions of $G$ given by $R\mapsto\mathscr{P}(R),\mathscr{Q}\mapsto E(\mathscr{Q})$: every equivalence relation determines a partition, which in turn determines the original equivalence relation.
In this exercise you’ve shown that if $f$ is any function with domain $G$, the fibres of $f$ form a partition of $G$, say $\mathscr{P}(f)$. This correspondence also goes both ways. Suppose that $\mathscr{Q}$ is a partition of $G$. Define $g_\mathscr{Q}:G\to\mathscr{Q}$ by letting $g_\mathscr{Q}(x)$ be the unique member of $\mathscr{Q}$ containing $x$; clearly $g_\mathscr{Q}$ is a function with domain $G$, and it’s very easy to check that
$\qquad(1)\qquad\mathscr{P}(g_\mathscr{Q})=\mathscr{Q}$ for any partition $\mathscr{Q}$ of $G$, and
$\qquad(2)\qquad g_{\mathscr{P}(f)}=f$ for any function $f$ with domain $G$.
That is, we have a natural bijection between partitions of $G$ and functions with domain $G$ given by $\mathscr{Q}\mapsto g_\mathscr{Q},f\mapsto\mathscr{P}(f)$: every partition determines a function that in turn determines the original partition.
And of course these give rise to a natural bijection between equivalence relations on $G$ and functions with domain $G$.
You’ll be using all three points of view when you study quotient groups.