TITLE: Existence of continuous function $f$ on $\Bbb R$ which vanishes exactly on $A\subset \Bbb R$
QUESTION [7 upvotes]: Question For any closed subset $A \subset \Bbb R$, does there exist a continuous function $f$ on $\Bbb R$ which vanishes exactly on $A$?

If we take $A=[a,\infty)$ or $(-\infty,a]$ or $\{a_1,a_2,\cdots,a_n\}$ or $\bigcup_{i=1}^{k} [a_i,b_i]$, then indeed we can have such a continuous function. But when I thought about cantor set or the set $\{\frac 1n : n \in \Bbb N\} \cup \{0\}$, which are closed in $\Bbb R$, I couldn't think of a continuous map vanishing exactly at these two sets.
How should I go about this problem?

REPLY [7 votes]: Yes there exists more than continuous  since A is closed, just take$$x\mapsto f(x) =\color{blue}{d(x,A) =\inf\{|x-y|: y\in A\}~~~~\text{Which is even Lipschitz function }}$$
  Since we konw that $$d(x,B)= 0\Longleftrightarrow x\in\bar B$$

It follow from triangular inequality that: 
$$\color{red}{|f(x)-f(y)|\le |x-y|}$$
Also see this: from here It is possible to write open subsets in the form $\Omega=\{x\in \mathbb{R}^N:g(x)>0 \}$