Delete duplicates after emails got redownloaded

I’m not sure what happened, but suddenly all my emails that were still on the mail server got downloaded again. I now have a huge list of duplicate messages.

How can I quickly clean up this mess?