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

Let $f$ be a function defined in $[0, 1]$ such that for each real number $y$ either there is no value of $x \in [0, 1]$ such that $f(x) = y$ or there are exactly two such values of $x$. Prove that
a) $f$ must be discontinuous on $[0, 1]$.
b) $f$ has an infinite number of discontinuities in $[0, 1]$.

Long back I had tried to solve the problem. I reasoned for a) as follows:
Suppose that $f$ is continuous and let $m, M$ be its minimum and maximum values on $[0, 1]$. Clearly if $m = M$ then $f$ is constant and clearly for every $x \in [0, 1]$ we have $f(x) = m = M$ which contradicts given property of $f$. So we must have $m < M$. Clearly there are now four distinct points $a, b, c, d$ in $[0, 1]$ such that $f(a) = f(b) = m, f(c) = f(d) = M$. By checking possible linear orderings of $a, b, c, d$ I could find a find a value of $y \in (m, M)$ for which there exist more than 2 values of $x$ such that $f(x) = y$. However this approach looks very clumsy and I suspect much simpler and easier proof is possible.
Part b) seemed bit complex and my only guess was that probably $f$ was discontinuous on each subinterval of $[0, 1]$ but I could not prove this claim (may be the claim is wrong!). Perhaps there is a simpler solution to this part of the problem.
Apostol's also asks to provide an example of such a function.
Any hints on the overall problem will be appreciated.

Update: After looking at Marc's answer, I think I have found a way to show that there are infinitely many discontinuities. Earlier the solution was a part of the question itself but it made the question look quite lengthy hence I moved it to a separate answer.
 A: 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$.
A: 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.
A: 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}.
$$
A: 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: There can be a somewhat different proof of this statement:
$a)$ $Claim:$ $\forall$${\epsilon}>0$ ${\exists}$ ${ {\delta}>0}$ with $\mid x-y \mid < \delta$ $\implies$ $\mid f(x)-f(y) \mid < \epsilon$ with $x,y  \in [0,1]$, $i.e$ $f$ is $continuous$ on $[0,1]$.
$Now,$ Choose a $neighborhood$ around $x$: $[x-{\delta_1}, x+{\delta_1}]$ with $\delta_1<\delta$, such that $f(x) = f(x+{\delta_1})$. And Clearly $f(x) {\ne} f(x-{\delta_1})$ as a particular value of $f$ can only take place $Twice$ on $[0,1].$
Now, with $y=x+\delta_1,$ we have $\mid f(x)-f(y) \mid = 0$ $(\because f(x) = f(x+{\delta_1}) )$, So any value greater than $0$ can be assigned to $\epsilon$. 
But for $y=x-\delta_1$;   $\mid f(x)-f(y) \mid > 0.$  Now, $choosing:$ $\epsilon < \mid f(x)-f(x-\delta_1) \mid$ leads to a $contradiction$, $i.e.$ $\exists$ at least one $\epsilon>0$ such that even with $\mid x-y \mid < \delta$, we get $\mid f(x)-f(x-\delta_1) \mid > \epsilon$
$\therefore$ $f$ is $discontinuous$ on $[0,1].$
$b)$ If There is no $x$ such that $f(x)=y$, it is trivial that $f$  has $infinite$ discontinuities on $[0,1]$, and if there are exactly two such values of y, with $f(x_1)=y=f(x_2)$ $with$ $ x_1 \ne x_2$ we can define the neighborhood around $x_1$: $[x_1-\delta_1, x_1+\delta_1]$ with $x_2 = x_1 + \delta_1$. And prove for the discontinuity at $x_1$. And this way, for all such values with two values of $f$ on $[0,1]$ we can prove for discontinuity.
A: This answer supplements @Marc van Leeuwen's answer for anyone who does not understand the last two paragraphs in his answer (based on the answer here).
Suppose $f$ has finitely many discontinuities. Let $S$ be set
\begin{equation*}
    S= \{0, 1\}\cup\{x\in [0, 1]: x\text{ is a discontinuity or local extremum}\}.
\end{equation*}
By the observations in @Marc van Leeuwen's answer, $S$ is a finite set. Let $T = f^{-1}(f(S))$; this is again a finite set (since each element in $f(S)$ has exactly $2$ elements in its preimage). Then $U = [0, 1]-T$ is the union of an odd number of open intervals (since $|T|$ is even) and the restriction of $f$ to each of these intervals is continuous and monotonic. Therefore, the image of each of intervals is also an open interval (the image of an open interval $I$ is $(\inf f(I), \sup f(I))$). Let $U = \bigcup_{i\in J}I_{i}$ and let $i_{0}\in J$. Using the property of the function, each point in $f(I_{i_{0}})$ must be in the image of exactly one other component interval in $U$. In other words, $f(I_{i_{0}}) = \bigcup_{j\in K} (f(I_{j})\cap f(I_{i_{0}}))$ for some $K\subseteq J-\{i_{0}\}$ and the terms in the union are pairwise disjoint open intervals. However, an interval cannot be the disjoint union of more than one interval, so in fact $K = \{j_{0}\}$ is a singleton and $f(I_{i_0})\subseteq f(I_{j_{0}})$. Applying the same argument to $f(I_{j_{0}})$ we get $f(I_{i_{0}}) = f(I_{j_{0}})$. This shows that we can pair the intervals that have the same image. However, we have reached a contradiction since the number of intervals is odd.
Example: let $(x_{n})_{n\in\mathbb{Z}^{+}}$ be any sequence in $(0, 1)$ and consider the function $f:[0, 1]\rightarrow [0, 1)$ defined by $f(1) = x_{1}$, $f(x_{n}) = x_{n+1}$ for all $n\in\mathbb{Z}^{+}$ and $f(x) = x$ for all other $x$. Then $f$ is a bijection from $[0, 1]$ to $[0, 1)$. Let $g: [0, 1)\rightarrow \mathbb{R}$ defined by $g(x) = g(x+1/2) = x$ for each $x\in[0, 1/2)$. Then $g\circ f$ is a function with the desired property.
