how to add urls dynamically into the sitemap

Looks like one of those is the canonical URL, can you take a look at the page source and check if is there a canonical tag?

    <link rel="canonical"...

Also there is a setting in yoast for the sitemap https://kb.yoast.com/kb/how-to-customize-the-sitemap-index/ and you can add your own sitemap too:
https://kb.yoast.com/kb/add-external-sitemap-to-index/

Note: Duplicate content will harm your SEO ranking