how to prove this ( about surjective functions and inverse functions) I  was making excercises for an upcoming exam and when trying to make this one I really got stuck.
The question is:
Prove that a function $f$ from $X$ to $Y$ is surjective if and only if there exists a function $g$ from $Y$ to $X$ with $g$ after $f$ is the unit function (the function that maps every $x$ on $x$).
My thoughts directly went to the inverse function but than I started having doubts because if the domain is bigger than the co-domain the inverse function would have to have more than one value for every input and than g wouldn't be a function.
My second thought was the same but with a reduced domain so that f becomes bijective but I do't think thats allowed in this question.
Does someone know a way to solve/proof this question?

You don't need to show the existence of an inverse, because the inverse may not exist, if $f$ isn't 1-1. You are supposed to show the right inverse, that is a function $g\colon Y\to X$ such that $f\circ g = \mathrm{id}_Y$.
You are right that $f^{-1}(\{y\})$ can have more than one element. Take just one of them, that is define $g(y)\in f^{-1}(\{y\})$.
