How to subtract one list from another, treating each elements as distinct, assuming the smaller list is a subset of the larger list?
Suppose that I have two lists of different sizes, the smaller of which is a subset of the larger, e.g.
a = {"A", "A", "A", "B", "B", "C"}
b = {"A", "B"}
How can I subtract b
from a
, treating each element as distinct, such that the result is {"A", "A", "B", "C"}
?
One solution I can think of is to use
Merge[{Counts[a], -Counts[b]}, Total]
then reconstruct the result somehow, but is there a simpler way?
Answer
Fold[DeleteCases[##, 1, 1] &, a, b]
{"A", "A", "B", "C"}
Comments
Post a Comment