Adding entries to the top level scope of a variable in Twig
I'm working on solving a similar problem and I've come up with a solution that stores a string of comma-delimited IDs in the session like this:
{% set displayedContentIds = craft.app.session.get('displayedContentIds')|split(',') %}

This code is added to all components on the page that are required to exclude content that's already been shown further up the page. I pass the array of IDs to the entry query for that block, if there are IDs to exclude:
{% set criteria = {
    section: 'mySection',
    limit: 5
} %}

{% if displayedContentIds|length &gt; 0 %}
    {% set criteria = criteria|merge({ id: 'and, ' ~ displayedContentIds|join(', not') }) %}
{% endif %}

{% set entries = craft.entries(criteria) %}

I output my entries as normal with entries.all(), then I update displayedContentIds with the just-displayed entry IDs:
{% set displayedContentIds = &quot;#{displayedContentIds},#{entries.ids()|join(',')}&quot; %}
{% do craft.app.session.set('displayedContentIds', displayedContentIds) %}

This is a bit crude if I'm honest, but it's the only way I can think of to update a variable inside a block and have the updated value available to blocks further down the page.