# A generalization of DeleteDuplicates[] that removes elements common in two lists [duplicate]

I have two lists $$A(k,m)$$ and $$B(k,m)$$ given by

A[k_, m_] := Prepend[Table[k/m + (l - 1)/(m + 1), {l, 0, m}], 1]
B[k_, m_] := Prepend[Table[(k + l)/m, {l, 0, m - 1}], (m + k)/m]


for any choices of $$m=2,4,6,\dots$$ and $$k=1,2,3,\dots,m$$.

What I'm trying to do is define new lists $$a(k,m)$$ and $$b(k,m)$$, which are the same as $$A$$ and $$B$$ except all common elements have been dropped. For example, consider

In[23]:= A[2, 4]
B[2, 4]
Out[23]= {1, 3/10, 1/2, 7/10, 9/10, 11/10}
Out[24]= {3/2, 1/2, 3/4, 1, 5/4}


We see each list has a common element of $$1/2$$ and $$1$$ so in this case $$a(2,4)=\{3/10,7/10,9/10,11/10\}$$ and $$b(2,4)=\{3/2,3/4,5/4\}$$.

I first tried writing DeleteDuplicates[Flatten[{A[2, 4], B[2, 4]}]] with the intent to then split the result back up into the two lists $$a$$ and $$b$$; however, I now see that DeleteDuplicates[] will not remove all occurences of a duplicated value. I also looked at the Drop[] function which may be helpful but still its not clear to me how I would use it.

• I found the answer here. I apologize for the duplicate. Commented Feb 28, 2022 at 14:18
• Try: common = Intersection[A[2, 4], B[2, 4]] and DeleteCases[A[2, 4], x_ /; MemberQ[common, x]] and similarly for the other case.
– Syed
Commented Feb 28, 2022 at 16:35

You can use Complement to define $$a$$ and $$b$$ based on your existing definitions of $$A$$ and $$B$$:

ClearAll[a, b]
a[k_, m_] := Complement[A[k, m], B[k, m]]
b[k_, m_] := Complement[B[k, m], A[k, m]]

a[2, 4]
b[2, 4]

(* Out:
{3/10, 7/10, 9/10, 11/10}
{3/4, 5/4, 3/2}
*)