Let $f: X \to X$ be such that $d(f(x), f(y)) = d(x, y)$ for all $x, y \in X$. To show that $f$ is onto. Let $(X, d)$ be a compact metric space. Let $f: X \to X$ be such that $d(f(x), f(y)) = d(x, y)$ for all $x, y \in X$. To show that $f$ is onto.
Since the function $f$ satisfies $d(f(x), f(y)) = d(x, y)$ for all $x, y \in X$ we can say that the function is uniformly continuous.
Now let us assume that $f$ is not onto then $f(X) \subset X$, a proper subset. Then how can I bring a contraction to show that $f$ will be onto??
 A: Let $x_0\in X$ and define
$$g:X\to\mathbb{R},\quad g(x)=d(f(x),x_0).$$
It is easy to show that $g$ is uniformly continuous. Since $X$ is compact, $g$ attains a minimum at some $y_0\in X$, i.e.
$$d(f(y_0),x_0)\leq d(f(x),x_0),\quad\forall x\in X.$$
We will show that this $y_0$ does it, i.e. $f(y_0)=x_0$. Suppose, for contradiction, that $f(y_0)\neq x_0$ and let
$$\epsilon_0:=d(f(y_0),x_0)>0$$
Define the sequence $(x_n)_{n=1}^\infty$ inductively by
$$x_1=f(x_0),\quad x_{n+1}=f(x_n).$$
Then, for any $m,n\in\mathbb{N}$, $m>n$, we have
\begin{align*}
d(x_m,x_n)&=d(f(x_{m-1}),f(x_{n-1}))=d(x_{m-1},x_{n-1})=\cdots=d(x_{m-n},x_0) \\
&=d(f(x_{m-n-1}),x_0)\geq d(f(y_0),x_0)=\epsilon_0
\end{align*}
Thus, $(x_n)_{n=1}^\infty$ cannot have any Cauchy subsequence. This contradicts that $X$ is compact, since a compact metric space is sequentially compact. So $f(y_0)=x_0$, and hence $f$ is onto.
A: Note that $f(X)\subset X$ is compact. Suppose we have $x\not\in f(X)$. Then by compactness we have $d(x,f(X))=\epsilon>0$. 
Now define $x_n=f^{n}(x)$, i.e. apply $f$ $n$-times. 
Note that $(x_n)_n\subset f(X)$, so $(x_n)_n$ must have a convergent subsequence (sequential compactness of $f(X)$).
We have $d(x,x_n)\ge \epsilon$ by definition of $\epsilon$. But by invariance of the metric under $f$ we also have
$$d(x,x_n)=d(f(x),f(x_n))=d(x_1,x_{n+1})=\cdots=d(x_{m},x_{n+m})$$
for every $m\in\mathbb{N}$. So $d(x_{m},x_{n+m})\ge \epsilon$ for all $n,m$, which implies that $(x_n)_n$ cannot have a convergent subsequence, contradiction!
