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?