It might be good to document here that this will do multiple passes over the data, and would thus work better on cached RDDs. Otherwise it seems like a fairly innocent operation that could take surprisingly long.