Use an optimized grouped-by implementation over large sequences.