TITLE: Bijective function on a dense set
QUESTION [7 upvotes]: Suppose X is a complete metric space, and $f:X↦X$ a continuous surjective function. Let D be a dense set. Suppose $f:D↦D$ is injective and $f^{-1}(D)=D$.   
Is $f$ injective ?
Is there a family of metric spaces where you can conclude  $f$ is injective?

REPLY [10 votes]: Sorry but I could not resist:
    
The map is defined on $[0,1]\times[0,1]$, and can be written as
$$
 f(x,y) = \begin{cases} 
             (x, (2-x)y) & \text{ if }y\leq 1/2, \\
             (x, xy+1-x) & \text{ if }y> 1/2.
         \end{cases}
$$
Just take $D$ to be the set of $(x,y)$ with rational coordinates in $(0,1)\times(0,1)$, then $f$ is  bijective on $D$ (because it can be easily inverted), but it is clearly not injective on $[0,1]\times[0,1]$.