diff --git a/lib/markdown2.py b/lib/markdown2.py
index 479663b0..245197f8 100755
--- a/lib/markdown2.py
+++ b/lib/markdown2.py
@@ -1811,7 +1811,7 @@ def _header_id_exists(self, text):
prefix = self.extras['header-ids'].get('prefix')
if prefix and isinstance(prefix, str):
header_id = prefix + '-' + header_id
- return header_id in self._count_from_header_id
+ return header_id in self._count_from_header_id or header_id in map(lambda x: x[1], self._toc)
def _toc_add_entry(self, level, id, name):
if level > self._toc_depth:
diff --git a/test/tm-cases/toc_duplicate_entries.html b/test/tm-cases/toc_duplicate_entries.html
new file mode 100644
index 00000000..dc051bf7
--- /dev/null
+++ b/test/tm-cases/toc_duplicate_entries.html
@@ -0,0 +1,5 @@
+
abc
+
+abc
+
+abc
diff --git a/test/tm-cases/toc_duplicate_entries.opts b/test/tm-cases/toc_duplicate_entries.opts
new file mode 100644
index 00000000..53a34fa2
--- /dev/null
+++ b/test/tm-cases/toc_duplicate_entries.opts
@@ -0,0 +1,6 @@
+{
+ "extras": {
+ "toc": {"depth": 3},
+ "header-ids": {"mixed": True}
+ }
+}
diff --git a/test/tm-cases/toc_duplicate_entries.text b/test/tm-cases/toc_duplicate_entries.text
new file mode 100644
index 00000000..c4f0e0a4
--- /dev/null
+++ b/test/tm-cases/toc_duplicate_entries.text
@@ -0,0 +1,3 @@
+# abc
+# abc
+# abc
diff --git a/test/tm-cases/toc_duplicate_entries.toc_html b/test/tm-cases/toc_duplicate_entries.toc_html
new file mode 100644
index 00000000..72262013
--- /dev/null
+++ b/test/tm-cases/toc_duplicate_entries.toc_html
@@ -0,0 +1,5 @@
+