Count pairs with equal Bitwise AND and Bitwise OR value | C # program to implement the above approach ; Function to count pairs in an array whose bitwise AND equal to bitwise OR ; Store count of pairs whose bitwise AND equal to bitwise OR ; Stores frequency of distinct elements of array ; Traverse the array ; Increment the frequency of arr [ i ] ; Traverse map ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int countPairs ( int [ ] arr , int N ) { int cntPairs = 0 ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( ! mp . ContainsKey ( arr [ i ] ) ) mp . Add ( arr [ i ] , 1 ) ; else mp [ arr [ i ] ] = mp [ arr [ i ] ] + 1 ; } foreach ( KeyValuePair < int , int > freq in mp ) { cntPairs += ( freq . Value * ( freq . Value - 1 ) ) / 2 ; } return cntPairs ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 1 , 2 , 2 } ; int N = arr . Length ; Console . WriteLine ( countPairs ( arr , N ) ) ; } }