Count quadruples ( i , j , k , l ) in an array such that i < j < k < l and arr [ i ] = arr [ k ] and arr [ j ] = arr [ l ] | C # program for the above approach ; Function to count total number of required tuples ; Initialize unordered map ; Find the pairs ( j , l ) such that arr [ j ] = arr [ l ] and j < l ; Elements are equal ; Update the count ; Add the frequency of arr [ l ] to val ; Update the frequency of element arr [ j ] ; Return the answer ; Driver code ; Given array [ ] arr ; Function call
using System ; using System . Collections . Generic ; class GFG { static int countTuples ( int [ ] arr , int N ) { int ans = 0 , val = 0 ; Dictionary < int , int > freq = new Dictionary < int , int > ( ) ; for ( int j = 0 ; j < N - 2 ; j ++ ) { val = 0 ; for ( int l = j + 1 ; l < N ; l ++ ) { if ( arr [ j ] == arr [ l ] ) { ans += val ; } if ( freq . ContainsKey ( arr [ l ] ) ) val += freq [ arr [ l ] ] ; } if ( freq . ContainsKey ( arr [ j ] ) ) { freq [ arr [ j ] ] = freq [ arr [ j ] ] + 1 ; } else { freq . Add ( arr [ j ] , 1 ) ; } } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 1 , 2 , 2 , 2 } ; int N = arr . Length ; Console . Write ( countTuples ( arr , N ) ) ; } }