Optimize combineAsArray by counting