How to remove the same elements of two 1-dim int tensors

a = torch.tensor( [10,2,5,1,9,7,8] )
b = torch.tensor( [1,9,8] )
I want to remove the same numbers as b from a.
What is the fastest way to do it?
The result tensor can be sorted or unsorted.
i.e. the result c is:
torch.tensor( [10,2,5,7] )
or
torch.tensor( [2,5,7,10] )

I can do this by:

from collections import Counter
ca = Counter(a.numpy())
cb = Counter(b.numpy())
c = torch.tensor( list( (ca-cb).elements() ) )

But, I am wondering if there is a better(faster) way to do it.

Thank you!

Hi Joseph!

At the cost of materializing a [len (a), len (b)] tensor, you can:

>>> import torch
>>> torch.__version__
'2.3.1'
>>> a = torch.tensor ([10, 2, 5, 1, 9, 7, 8])
>>> b = torch.tensor ([1, 9, 8])
>>> a[(a == b.unsqueeze (-1)).sum (dim = 0) == 0]
tensor([10,  2,  5,  7])

Best.

K. Frank

Thank you, Frank! I will do the speed benchmark to choose the faster one.