Can a continuous real function take each value exactly 3 times? Let $f: \mathbb{R} \to D \subseteq \mathbb{R}$ be a continuous function. Is there a function $f$ that satisfies the following property? $\forall y \in D$, there are exactly 3 $ x_1,x_2,x_3 \in \mathbb{R}$ such that $f(x_1) = f(x_2) = f(x_3) = y$.

Let $\phi\colon[0,1]\to\mathbb{R}$ be defined as $\phi(x)=1-3\,|x-1/3|$ and let
$$
f(x)=\sum_{k\in\mathbb{Z}}(\phi(x-k)-k)\,\chi_{[k,k+1]}(x).
$$
where $\chi_A$ is the characteristic function of the set $A$.

