I've been trying to prove the following for a while, out of curiosity. For any (possibly discontinuous, etc) function $f:A\to A$ such that $$ f^n(x) = f(x),\quad \forall x\in A, n\in \mathbb{N} $$ where $f^n$ is the $n$-repeated application of the function $f$ on its argument, then $f$ is either the identity mapping or constant everywhere.
Is this statement true? It feels like it might be, at a first glance, but I have little intuition for these types of statements, so it's unclear if there exists a weird counterexample from adding Choice into the mix.
For now, we can say, for example, that $A = \mathbb{R}$ (though I'm quite curious about the general case as well).
The answer is no. Take $A = \Bbb R$ and $f(x) = |x|$.
In general, we can say that if $f(f(x)) = f(x)$, then $f$ is equal to the identity over the image of $f$. In particular: consider any $y$ in the image of $f$. Then $y = f(x)$ for some $x \in A$, and we have $$ f(y) = f(f(x)) = f(x) = y $$ If $f$ is surjective, then the image of $f$ is $A$, and so $f$ must be the identity map over $A$.
Note that if $f$ is injective, then there exists a function $g(x)$ such that $g(f(x)) = x$, and we have $$ f(f(x)) = f(x) \quad \text{for all }x \in A \implies \\ g(f(f(x)) = g(f(x)) \quad \text{for all }x \in A\implies \\ f(x) = x \quad \text{for all }x \in A $$