Counting common values in two arrays
Try the following algorithm:


For $1 \leq i \leq m$: $C[A[i]] = 0$
For $1 \leq i \leq m$: $C[B[i]] = 1$
Initialize answer to $0$
For $1 \leq i \leq m$:


If $C[A[i]] = 1$ then $C[A[i]] = 2$ and increment answer

Return answer