What was the final run time?
4 seconds in google apps script by handling indices in loops instead of rearranging elements
4 seconds in google apps script by handling indices in loops instead of rearranging elements