What I did was to preprocess the data. I created a @bools2 that maps each boolean to the sets it is in. @sets2 contains the number of booleans in each set. And instead of splicing a set from @sets, I delete it:
use strict; use warnings; use List::Util qw[shuffle]; our $N //= 1e3; our $S //= 1e4; our $Z //= 5; my @bools = (0) x $N; my @sets = map [map int(rand $N), 1 .. 1 + int(rand $Z)], 1 .. $S; my @someOrder = shuffle 0 .. $N - 1; # # Preprocessing # my @bool2; my @sets2; for (my $i = 0; $i < @sets; $i ++) { push @{$bool2[$_]}, $i for @{$sets[$i]}; $sets2[$i] = @{$sets[$i]}; } # # Main loop # foreach my $next (@someOrder) { foreach my $set_nr (@{$bool2[$next]}) { delete $sets[$set_nr] unless --$sets2[$set_nr]; } }
I think this algorithm is O(k), where k is the sum of the number of elements in each set.

In reply to Re: Better algorithm or data structure? by JavaFan
in thread Better algorithm or data structure? by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.