• Would this not benefit from using a tally table? It would save the loops and missing IDs could be simply omitted?

    ...