The title might be misleading, but whether such a function exists is what boggles me about the following problem:


Let $f:\mathbb{R}\rightarrow \mathbb{R}$ be a continuous function such that for all $a<b $ satisfying $f(a)=f(b)$, there exists $c$ in $(a,b)$ such that $f(a)=f(c)=f(b)$.


Prove that $f$ is monotonous on $\mathbb{R}$.


What I'm intuiting about this problem is that for every such pair $a, b$ the function f is constant on $[a,b]$. Therefore, $f$ could have no local extremum. However, I'm not sure how to go about proving this. Is the set $X=\{x\in [a,b]| f(x)=f(a)\}$ be dense in $[a,b]$? If it is, how could I prove it? What also troubles me, though, is the existence of nowhere-monotone functions such as the Weierstrass function. Does the Weierstrass function satisfy the problem condition?


Furthermore, I'd like to be able to prove that an arbitrary horizontal line $g(x)=u, u \in \mathbb{R}$ either intersects f at a single point, or at a compact interval $[a\_{1},b\_{1}]$ $(a\_{1}<b\_{1})$. 


I'm not sure if these two conditions are enough to prove that the function is monotonous. 


What is the best approach towards proving this problem? Am I on the right track?



Continuous non-constant function with infinite intersections with horizontal line on a compact interval?
Preimage $f^{-1}(v)$ of any value $v$ is a closed set, hence its complement $U(v)$ is open. This open set $U(v)$ is a disjoint union of intervals. If some interval is finite, say $(a,b)$, then $f(a)=f(b)=v$, but $f(c)\ne v$ for $a<c<b$. So, all intervals in $U(v)$ are infinite. Hence preimage of $v$ is connected: it is either a segment (possibly a single point), or a ray. Now we prove that $f$ is monotone. It suffices to prove that $f$ is monotone on any three points $\{a<b<c\}$. Assume the contrary, for example, $f(b)>v:=\max(f(a),f(c))$. We see that $f^{-1}(v)$ is not connected: it contains points on $[a,b)$ and $(b,c]$, but does not contain $b$.


