Factor out "_remove_duplicate".
Reorganise loop of "precompute_muex".