Likewise, the `return_inverse` option to `np.unique` will accomplish this in a single pass through the data.