I'm having a confusion over the veracity of the statement that a function that satisfies the Intermediate Value Theorem and takes each value only finitely many times is continuous.
I've seen from a problem in Spivak's Calculus that this statement is true.
Proof: For the case where $f$ takes on each value only once: If $f$ is not continuous, there exists $\epsilon gt 0$ such that there are $x$'s arbitrarily close to $a$ with $f(x)\gt f(a)+\epsilon$, or $f(x)\lt f(a)-\epsilon$. Say the first, then there are $x$'s arbitrarily close to $a$ with $x\gt a$ or $x\lt a$, with $f(x)\gt f(a)+\epsilon$. Say the first. Then by IVT, there is $x'\in (a,x)$ with $f(x')\lt f(a)+\epsilon.$ Also by assumption, there is a $y\in (a,x')$ with $f(y)\gt f(a)+\epsilon$. So by IVT, we can find a $x_1\in (y,x'),$ and $x_2\in (x',x)$ with $f(x_1)=f(x_2)=f(a)+\epsilon$, which is a contradiction.
The proof for the case where $f$ takes on each value only finitely many times proceeds by inductive reasoning to the above proof.
However, I just came across a problem that states that if $h:[0,1]\to R$ takes each value exactly twice, then $h$ cannot be continuous on $[0,1]$. The proof to this problem contradicts the above statement.
Proof: Suppose that $h$ is continuous.
(1) By the Min-Max Theorem, $\exists c_1\in [0,1]$, which attains the maximum value of the function. Since each value is taken exactly twice, there is another $c_2\in [0,1]$, say $c_1\lt c_2$ such that $h(c_1)=h(c_2)$.
Now if $0\lt c_1$, then we can choose $a_1,a_2\in (0,1)$ and a real number $k$ such that $0\lt a_1\lt c_1\lt a_2\lt c_2$ and $h(a_1)\lt k \lt h(c_1), h(a_2)\lt k \lt h(c_2)$. Then since $h(c_1)=h(c_2)$ are maximum and $h$ is continuous, by IVT there are $b_i\in (0,1)$ such that $h(b_i)=k$ and $a_1\lt b_1\lt c_1\lt b_2\lt a_2\lt b_3\lt c_2$. This is a contradiction. Hence $c_1=0.$
(2) Now by the same reasoning as above, we can show that there are $d_1, d_2$ for which $h$ attains the minimum and $d_1=0, d_2=1$.
Hence by (1) and (2) h is a constant function, which is a contradiction to the assumption.
Now I don't see any errors in the reasoning of either proof, but the conclusions seem contradictory. How can I reconcile this situation?