For each $y \in \mathbb{R}$ either no $x$ with $f(x) = y$ or two such values of $x$. Show that $f$ is discontinuous.

First observe that a function $f$ that is continuous on an open interval $I$ and has no local extrema in $I$ (points where the value is the minimum or maximum of the values in a neighbourhood) must be strictly monotonic (increasing or decreasing) on $I$. To show this, assume for $a<b<c$ that either both $f(b)\leq f(a)$ and $f(b)\leq f(c)$ or both $f(b)\geq f(a)$ and $f(b)\geq f(c)$; then some minimum respectively maximum of $f$ on the compact set $[a,c]$ will lie in its interior and therefore be a local extremum, contradicting the hypothesis.

Next, a continuous function on an open interval $I$ that never takes the same value more than twice (and in particular cannot be locally constant anywhere) cannot have two local extrema of the same nature (both minimum or both maximum). Again an easy contradiction is obtained after assuming for instance that $a<c$ are local minima with $f(a)\leq f(c)$ say, $b\in(a,c)$ a maximum of $f$ on $[a,c]$, and $d>c$ a point of $I$ with $f(c)<f(d)<f(b)$; then $f$ also takes the value $f(d)$ on each of $(a,b)$ and $(b,c)$.

This already allows proving point a) by deriving contradictions for each of the cases of a continuous function having zero, one or two local extrema in $(0,1)$.

Now consider the case of a function$~f$ with finitely many discontinuities. By cutting the interval at each discontinuity and at each of the local extrema of$~f$ (which are finite in number by the above arguments), we partition $[0,1]$ into $n$ open intervals $I_i$ on each of which $f$ is continuous and strictly monotonic, and $n+1$ isolated points. The image of each $I_i$ is an open interval; we can define a multigraph on the set of endpoints of all $f(I_i)$, with an edge from $\inf(f(I_i))$ to $\sup(f(I_i))$ for each$~i$. Because of the property required for$~f$, local consideration of the closure $J$ of $f(\bigcup_iI_i)$ near any vertex shows that only four kinds of vertices are possible: if the vertex is on the boundary of$~J$ then it either has two ingoing or two outgoing edges, otherwise (the vertex is in the interior of$~J$) it either has two ingoing and two outgoing edges, or one incoming and one outgoing edge. The final case is the only one in which the vertex is in (the interior of) $f(I_i)$ for some$~i$, and it will be so for exactly one$~i$; let $V_1$ be the set of these vertices.

Our contradiction will come from a parity condition, namely that $|V_1|\equiv n\pmod2$. This congruence is obtained by counting the endpoints of the $n$ edges of our multigraph modulo$~2$: the vertices of $V_1$ contibute $1$ to this count and all other vertices contribute $0$. Now the values of $f$ on the $n+1$ isolated points must be such that each vertex of $V_1$ is used exactly once, and any other value used (which must be outside $f(\bigcup_iI_i)$) must be so exactly twice. But parity shows this is not possible.


a) Suppose to the contrary that $f$ is continuous. The maximum value $M$ of $f$ on $[0,1]$ is taken on twice, say at $a$ and $b$, where $a\lt b$.

Suppose that $0\lt a\lt b\lt 1$. Let $m_1$ be the minimum of $f$ in the interval $(a,b)$, and let $c$ be a point in $(a,b)$ where this minimum is attained. Let $\epsilon=\min(M-f(0), M-m_1, M-f(1))$.

Consider the number $M-\frac{\epsilon}{2}$. By the Intermediate Value Theorem, there is an $x_1$ in the interval $(0, a)$, and $x_2$ in the interval $(a,c)$, and $x_3$ in the interval $(c,b)$, and an $x_4$ in the interval $(c,1)$ such that $f(x_i)=M-\frac{\epsilon}{2}$ for $i=1,2,3,4$. This contradicts our assumption that each value is taken on exactly twice.

A very similar argument can be given if $a=0$ and $b\ne 0$, and also if $a\ne 0$ and $b=1$. The only difference is that we find values that are taken on at least $3$ times.

Suppose now that $a=0$ and $b=1$. Then there are $a,b$ with $0\lt a\lt b\lt 1$ at which $f$ attains a minimum. Now we can use essentially the same method as the one we used when the maximum is reached at points strictly between $0$ and $1$.


This is a solution to part b) of the question and it is in part inspired by the answer from Marc and discussion with him.


Pairing Discontinuities and Endpoints: Let us assume on the contrary that there are a finite number of discontinuities $x_{1}, x_{2}, \ldots, x_{n}$. Then by the given property of $f$ (that it takes every value twice) we have numbers $y_{i}$ such that $f(x_{i}) = f(y_{i})$. Let us consider set $$A = \{x_{1}, x_{2}, \cdots, x_{n}, y_{1}, y_{2}, \cdots, y_{n}\}$$ On the face of it there seem to be $2n$ elements in this set $A$. However it may happen that we have $f(x_{i}) = f(x_{j})$. In this case we will have $y_{i} = x_{j}, y_{j} = x_{i}$. Thus there will be $2$ duplicates which will be ignored during counting members in $A$. Effectively this means that $A$ has even number of distinct points. It may happen that one or both of the points $0, 1$ are not in $A$. Let's find points $a, b$ such that $f(a) = f(0)$, $f(b) = f(1)$. It can also happen that $a, b$ turn out to be $0, 1$. Add these points $0, 1, a, b$ also in $A$. Now the set $$A = \{0, 1, a, b, x_{1}, \ldots, x_{n}, y_{1}, \ldots, y_{n}\}$$ has an even number of distinct points. Removing them from $[0, 1]$ will partition the interval into odd number of open subintervals such that $f$ is continuous in each of these open subintervals.

Pairing Local Extrema: Next by the nature of the function $f$ (every value taken twice) number of local extrema in each of these open intervals is finite (each open sub-interval of continuity of $f$ can have "one local maximum", or "one local mimimum" or "one local maximum and one local minimum"). Let these points of local extrema be $c_{1}, c_{2}, \ldots c_{k}$. Note that none of these points are in set $A$ and for every $x \in A$ we have a $y \in A$ such that $f(x) = f(y)$. Hence the corresponding points $d_{i}$ with $f(c_{i}) = f(d_{i})$ are also not in $A$. Note that the $d_{i}$ may or may not be local extrema of $f$. And like in the case of $x$'s and $y$'s if we take $c$'s and $d's$ together to form set $$B = \{c_{1}, c_{2}, \ldots, c_{k}, d_{1}, d_{2}, \ldots, d_{k}\}$$ the set $B$ will have even number of distinct elements. Let $S = A \cup B$ and since $A \cap B = \emptyset$, it follows that the set $S$ will also have even number of distinct points. And to emphasize $S$ has another property like $A$ or $B$ namely that for each $x \in S$ we have a $y \in S$ with $f(x) = f(y)$.

Final Step: Removing points of $S$ from interval $[0, 1]$ will generate an odd number of disjoint open sub-intervals and $f$ will be continuous and monotone in each of these open subintervals. Clearly the image of $f$ on such open subintervals will also be an open interval (say $I_{r}$). Because $f$ takes every value twice each of the images $I_{r}$ will be paired with another $I_{s}$ (i.e. $I_{r} = I_{s}$). But since the number of such intervals is odd, the pairing is not possible and we get a contradiction.


I don't know how to prove the part b), but I find a example of such function.

Let $A=\{\frac{1}{2}+\frac{1}{2n} : n\in\Bbb Z-\{0\}\}\cup \{\frac{1}{2}\}$ and $A=A_1\cup A_2$ be a partition of $A$. Enumerate $A_1=\{b_n:n\in\Bbb{N}\}$ and $A_2=\{c_n:n\in\Bbb N\}$. Define $f$ such that

$$ f(x)=\begin{cases} n+1 & \text{if $x=b_n$ or $x=c_n$}\\ x(1-x) & \text{otherwise} \end{cases}. $$