There's probably a simpler way to do this but my solution was literally to create an array of urls as they are output and each iteration does an in_array() check to see if the url has already been output before it continues. If it's a duplicate it's skipped and so on.
Simples...
