Let $f$ be a function having the intermediate value property (it is not necessarily continuous) and $y \in \mathbb{R}$ is fixed. It is given that $$M=\{x \in \mathbb{R} \mid f(x)=y \}$$ is bounded above and not empty. Prove that $\sup M \in M$.
I tried to prove this by contradiction. Let $\alpha = \sup M $ and suppose $\alpha \not \in M$, i.e. $f(\alpha)\neq y$ and let $\lambda$ be between $f(\alpha)$ and $y$. I then took $(x_n) \subset M$ such that $x_n \to \alpha$. By the intermediate value property, since $f(x_n)=y$, there must be some $c_n \in (x_n,\alpha)$ such that $f(c_n)=\lambda$. However, I don't see how to obtain the contradiction from this point on.
The statement is false. Take, for instance $$\begin{array}{rccc}f\colon&\mathbb R&\longrightarrow&\mathbb R\\&x&\mapsto&\begin{cases}\sin\left(\frac1x\right)&\text{ if }x<0\\1&\text{otherwise.}\end{cases}\end{array}$$ Then $f$ satisfies the intermediate value property, $$\sup\left\{x\in\mathbb{R}\,\middle|\,f(x)=0\right\}=0,$$ but $f(0)=1\neq0$.