Replacing / mapping values of tensor to be contigous integers

Hello,
I have a tensor with values, say,
tensor([ 0, 0, 1, 1, 3, 3, 7])
and would like to have a tensor that replaces these numbers with contiguous integers.
I want the above to become
tensor([ 0, 0, 1, 1, 2, 2, 3])

How do I do that as effectively as possible?

If your original tensor is sorted (or if you could sort it), the reverse indices of unique would give you the desired tensor:

x = torch.tensor([ 0, 0, 1, 1, 3, 3, 7])
_, idx = x.unique(sorted=True, return_inverse=True)
print(idx)
> tensor([0, 0, 1, 1, 2, 2, 3])