Depends on where you are rendering the Markdown.
In Jekyll with kramdown you can use {.toc} to get an automatically generated table of contents.
On Github this doesn't exist, so you have to use an external tool like https://ecotrust-canada.github.io/markdown-toc/ (Google "markdown generate TOC" for alternatives)