The only way I've found, using macOS' standard Mail app and a simple duplicate file software, is:
- Export all your emails as .eml (select, and drag-drop emails into a Finder folder, from the Apple Mail app). See answer https://apple.stackexchange.com/a/350900/278784 for more details on how to export, but also import back after deletions are done. Note that when selecting a lot of emails and dragging to a folder, in my case it took a very long time with seemingly nothing happening until the .eml files were actually exported.
- Use a duplicate file software, I use Duplicate File Finder from the App Store, which is free for exact duplicate file deletion. Delete all duplicates that it finds. This worked great for me, it found all duplicates (I think, because I have >100 000 emails so...).
- Import back the remaining .eml files, also using the details from the answer linked above. You could then delete all the original emails, or do it before you import back the cleaned up emails, but make sure you have a back up.
As a benchmark, exporting ~10 000 emails into .eml files in a folder took me over 2 hours. During that time, I could check if something was actually going on by looking in Activity Monitor: Mail.app was using around 80% of a processor core, even dropping later to below 30% after a while. Otherwise Mail.app when not doing anything was using only way under 1%.