How to get common values between two multi sets?
Since you have multiple repetitions in A and B, and these two vectors are of different lengths (5913 and 3733), it is expected that vector C and D are of different size. However, C and D might contain the same unique elements.

Let's take the example proposed by Biswajit Banerjee:

A = c(1,1,2,2,3,3,4,4,5,5,6,6,9,11,15,17,19,19)
B = c(1,2,2,2,3,3,3,4,5,6,7,8,8,8)

C= A[(A %in% B)]
D = B[(B %in% A)]

all(unique(C) == unique(D))
TRUE


You could verify that your vectors C and D of length 2007 and 126 contains the same unique values. Does it solve your problem ?