You could use torch.unique with return_counts=True, check for each index with counts > 1, and grab the index. Once you have all repeated indices, you could use it to calculate the position of these indices and use this to calculate the mean of v.