Divide an array into groups.