here's how the things should look (pseudo-syntax):
set g = your init (from above)
if number of units in g == 0 return <no unit>
set closest_unit = get first unit from g
set min_distance = distance for unit closest_unit
call remove unit from group( closest_unit, g )
loop: for each unit in g /*enumeration, not random*/
set t_unit = picked unit
set t_distance = distance for unit t_unit
call remove unit from group( t_unit, g )
if t_distance < min_distance then
set min_distance = t_distance
set closest_unit = t_unit
endif
endloop
return closestunit