Give an example of a continuous function $f:R\rightarrow R$ which attains each of its values exactly three times. 
*

*Give an example of a continuous function $f:R\rightarrow R$ which attains each of its values exactly three times. 


Ed.: answered by the duplicate above


*

*Does there exist a continuous function $f:R\rightarrow R$ which attains each of its values exactly two times?


Ed.: answered by $f: \mathbb{R} \to \mathbb{R}$ that takes each value in $\mathbb{R}$ twice
 A: $$f(x)=2\left\lfloor\frac{x}{3\pi}\right\rfloor-\cos\left(3\pi\left\{\frac{x}{3\pi}\right\}\right)\tag{1}$$
is a differentiable function that attains any real value in exaclty three points, whose graphics is the following one:
$\hskip2in$
If in $(1)$ you replace "$3$" with the positive odd integer $2m+1$, you get a $C^1$-function that attains any real value in exactly $2m+1$ points. You can also take:
$$f(x) = T_{2m+1}\left(x-2\left\lceil\frac{x-1}{2}\right\rceil\right)+2\left\lceil\frac{x-1}{2}\right\rceil,$$
where $T_{2m+1}$ is the $(2m+1)$-th Chebyshev polynomial of the first kind. This is a $C^1(\mathbb{R})$ function, too.

You cannot have a continuous function $g$ that takes any value in $g(\mathbb{R})$ exactly twice. Such function cannot be monotonic, hence must have a point $x_0$ of local maximum/minimum and another point $x_1\neq x_0$ for which $g(x_1)=g(x_0)$. Then, by continuity, there exists a constant $G$ sufficiently close to $g(x_0)$ but different from $g(x_0)$ such that there are two points $y_1,y_2$ in a neighbourhood of $x_0$ (because $x_0$ is a maximum/minimum), and at least one point $y_3$ in a neighbourhood of $x_1$ such that 
$$ g(y_1)=g(y_2)=g(y_3)=G,$$
contradiction.
