$f(f(f(x)))=x$, does it necessarily imply $f(x)=x$? 
[TIFR GS-2017] Let $f$ be a continuous function on $\mathbb R$ satisfying the relation
$$f(f(f(x))) = x\ \forall x \in \mathbb R$$ Prove or disprove that $f$ is the identity function.

There is a solution to this question in here, but I was trying in this way...
Suppose $f$ is continuous and not identity function. Then we can find some point $c \in \mathbb R$ such that there is a neighbourhood $N_\delta(c)$, where $f(x) \neq x\ \forall x \in \mathbb N_\delta(c)$.
Then two cases arise.
$\underline{Case (i)}:$ $f(f(x))=f(x)$, but that means $f(f(x))=f(x)\neq x$, hence $f(f(f(x))) = f(f(x)) =f(x) \neq x$.
Hence we have a contradiction.
$\underline{Case (ii)}:$ $f(f(x)) \neq f(x)$.
Suppose $p$ be such a point where it holds. Then $f(f(p)) \neq f(p)$. Suppose $m \in \mathbb R$ s.t. it is in between $f(f(p))$ and $f(p)$. Then by IMVP, there is some $q$ between $p$ and $f(p)$ s.t. $f(q) = m$.
After this scene, I am out of ideas to proceed further. We can't say that $f(p) \in N_\delta(c)$. Any help would be appreciated.
 A: Just for others reading this post, the condition of continuity on $f(x)$ is extremely important to this question. If this condition is not included, then $f(x)$ need not be the identity. In fact, going down the rabbit hole of linked questions, the accepted answer at the bottom gives the example
$$f(x)=\frac{x-\sqrt{3}}{x\sqrt{3}+1}$$
which satisfies $f(f(f(x)))=x$. However, this obviously is not continuous on $\mathbb{R}$ and as such is not a counterexample to the question presented in OP's post. To answer this question, consider the function $g(x)=f(x)-x$. Since both $f(x)$ and $x$ are continuous, this function is a continuous function. We will show that it is identically zero. First, note that there must be $x_0$ such that $g(x)=0$. If this were not the case, then either $f(x)>x$ or $f(x)<x$ for all $x\in\mathbb{R}$. In the first case, this would then imply
$$x=f(f(f(x)))$$
$$\Rightarrow f(f(x))<x$$
$$\Rightarrow f(x)<x$$
$$\Rightarrow x<x$$
which is obviously a contradiction. The case where $f(x)<x$ follows in the same manner. Now, the places where $g(x)>0$ form a countable sequence of open intervals. There are four possibilities for the open interval with $x_0$ as an endpoint:
$$(x_0,\infty)$$
$$(x_0,b)$$
$$(-\infty,x_0)$$
$$(a,x_0)$$
(where $a<x_0<b$) and it is possible that multiple of these apply. In addition, there are the possibilities  that $g(x)=0$ for $x\in [a,b]$ where $a<x_0<b$, $g(x)=0$ for $x\in [a,\infty)$ where $a<x_0$, $g(x)=0$ for $x\in (-\infty,b]$ where $x_0<b$, or $g(x)=0$ for $x\in\mathbb{R}$. However, as the argument for these seven cases (as the eight case is what we are trying to prove) are basically identical, we will only prove the first possibility. Thus, consider the interval $(x_0,\infty)$. In this interval, $g(x)>0$ or $g(x)<0$. In the first case, let $\epsilon_n>0$ be any sequence that approaches $0$. Then eventually
$$x_0+\epsilon_n=f(f(f(x_0+\epsilon_n)))>f(f(x_0+\epsilon_n)>f(x_0+\epsilon_n)>x_0+\epsilon_n$$
As this is a contradiction, we conclude that this open interval cannot actually exist and therefore $g(x)=0$ for $x\in (x_0,\infty)$. This argument can be expanded to the other four cases above, and from there expanded by induction to the entire real number line. We conclude that $f(x)=x$.
A: Continuity is important
As the others pointed out, the requirement that the function f is continuous is very important here.
If we formally answer not the question in your text, "Let f be a continuous function ...", but the question in the title, where continuity is not mentioned, then many solutions are possible.
Here is an example. Unlike other examples, where function is undefined in some points, following function is defined for every value of x:
$$f(x) = floor(x) + frac(frac(x) + 1/3)$$

For any x following holds:
$$f(f(f(x))) = x$$
But this function is not continuous and thus does not prove anything.
Proof of not existence
You wanted to have a proof based on contradiction. It is possible, but not exactly the way you started. First I'd suggest to show that if f(x) exists, then it must be monotone. Then you can consider separate cases how f(x) can be located relative to y(x) = x. There are only a few types possible. If you look at each case closer, you will come to contradiction.
