How to mark duplicates in a tensor

Given an int tensor of arbitrary shape, I would like to mark the first occurrence of every unique value as 0, and all the rest elements as 1, like below

x = torch.tensor([1,2,3,1,3,4,5,2])
y = mark_duplicates(x)

y would be [0,0,0,1,1,0,0,1], which has the same shape as x.

If torch.unique can return unique_indices like np.unique then the above can be achieved easily, but as far as I know it doesn’t. Is there an easy alternative?