Set multiple entries from different categories merge or join?
Use Craft's indexBy and index them by ID. If then you use Twig's merge function the duplicates will be removed and everything will be available in one array.

And may I suggest that if you're going to have a lot of logics in your templates, you'd better move this to a plugin and rewrite it in PHP. Templates should mostly be about templating, not about logics.