Count number of common elements between two arrays by using Bitset and Bitwise operation | Python3 implementation of the approach ; Function to return the count of common elements ; Traverse the first array ; Set 1 at ( index ) position a [ i ] ; Traverse the second array ; Set 1 at ( index ) position b [ i ] ; Bitwise AND of both the bitsets ; Find the count of 1 's  ; Driver code
MAX = 100000 NEW_LINE bit1 , bit2 , bit3 = 0 , 0 , 0 NEW_LINE def count_common ( a , n , b , m ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT global bit1 , bit2 , bit3 NEW_LINE bit1 = bit1 | ( 1 << a [ i ] ) NEW_LINE DEDENT for i in range ( m ) : NEW_LINE INDENT bit2 = bit2 | ( 1 << b [ i ] ) NEW_LINE DEDENT bit3 = bit1 & bit2 ; NEW_LINE count = bin ( bit3 ) . count ( '1' ) ; NEW_LINE return count ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , 4 , 7 , 2 , 3 ] ; NEW_LINE b = [ 2 , 11 , 7 , 4 , 15 , 20 , 24 ] ; NEW_LINE n = len ( a ) ; NEW_LINE m = len ( b ) ; NEW_LINE print ( count_common ( a , n , b , m ) ) ; NEW_LINE DEDENT