All continuous $f$ such that $\sin(f(x)) = \sin(x)$

For each $x \in \Bbb R$ you have $$ \sin(f(x)) = \sin(x) \Longleftrightarrow \begin{cases} f(x) = g_k(x) := x + 2k \pi \text{ for some } k \in \Bbb Z \\ \text{or} \\ f(x) = h_k(x) := -x + (2k+1) \pi \text{ for some } k \in \Bbb Z \\ \end{cases} $$ Those two families of curves intersect exactly at the points $x_i = (i + \frac 12)\pi$, $i \in \Bbb Z$.

Now suppose that $x_0 \in I = (x_{i-1}, x_{i})$, and $f(x_0) = g_k(x_0)$ for some $k$. Then there is some $\varepsilon > 0$ such that $|g_l(x_0) - f(x_0)| > \varepsilon $ for all $l \ne k$ and $|h_l(x_0) - f(x_0)| > \varepsilon$ for all $l$. $f$ is continuous, so there is a $\delta > 0$ such that $|f(x) - f(x_0)| < \varepsilon$ for $x \in (x_0 - \delta, x_0 + \delta)$ and therefore $f(x) = g_k(x)$ in that interval.

This shows that the set $\{ x \in I \mid f(x) = g_k(x) \}$ is open. Since it is closed as well and intervals are connected, we must have $f(x) = g_k(x)$ for all $x \in I$.

We have therefore shown: On each interval $(x_{i-1}, x_{i})$, $f$ is equal to some $g_k$ or some $h_k$.

In other words, $f$ is piecewise linear with $f(0) = k \pi$, $f'(0) = (-1)^k$ for some $k \in \Bbb Z$ and slope $+1$ or $-1$ on each interval $[(i - \frac 12)\pi, (i + \frac 12)\pi]$.


I used product and sum formulas for $\sin(x)$:

$$ \sin p + \sin q = 2\sin \left(\frac{p+q}{2}\right)\cos \left(\frac{p-q}{2}\right) $$

Which using in the problem: $$ \sin f(x) = \sin x \Leftrightarrow \sin f(x) + \sin (-x) =0 \Leftrightarrow $$ $$ 2\sin\left(\frac{f(x)-x}{2}\right)\cos\left(\frac{f(x)+x}{2}\right) = 0$$

Thus, let $k$ be an integer:

$$ \sin\left(\frac{f(x)-x}{2}\right) =0 \Leftrightarrow \frac{f(x)-x}{2} = k\pi \Leftrightarrow f(x) = x + 2k\pi $$

Or $$ \cos\left(\frac{f(x)+x}{2}\right) =0 \Leftrightarrow \frac{f(x)+x}{2} = \frac{\pi}{2} + k\pi \Leftrightarrow f(x) = -x + \pi + 2k\pi $$

However, $f$ can assume any of the two forms above depending on the interval. To satisfy the continuity, we require that (for $k$ and $q$ integers):

$$ x + 2k\pi = -x +\pi + 2q\pi \implies x = \left(q-k+ \frac{1}{2} \right)\pi = \left(j + \frac{1}{2} \right)\pi $$

Which will be the points in which $f$ can "changes" its form. Then,

$$f(x) = \begin{cases} x + 2k\pi, x \in \left[(j + \frac{1}{2} )\pi, (j +1 + \frac{1}{2} )\pi\right] \\ -x + \pi + 2k\pi, x \in [(i + \frac{1}{2} )\pi, (i +1 + \frac{1}{2} )\pi] \end{cases} $$

For some $k$ integer. Besides, $i \in A$ and $j \in B$, such that $A\bigcap B = \emptyset$ and $A \bigcup B = \mathbb{Z}$.