You could do it in a couple a ways:
1. you could do a nested loop,
Pick one ship and compare if it is within the distance of another ship of different faction.
2. You could use an invisible object,
I prefer this, use a sprite for example a circle of a certain radius.
You could set to invisible
Pick a ship and set the position of this sprite to the position of that ship.
Check if other ships overlap with the sprite used
For each of those ships check for the faction and the distance
If the distance is lesser store the UID.
I am also wondering if Pick nearest System event would work for you.