Given an array of pairs , find all symmetric pairs in it | C # program to find all symmetric pairs in a given array of pairs ; Print all pairs that have a symmetric counterpart ; Creates an empty hashMap hM ; Traverse through the given array ; First and second elements of current pair ; Look for second element of this pair in hash ; If found and value in hash matches with first element of this pair , we found symmetry ; Else put sec element of this pair in hash ; Driver code
using System ; using System . Collections . Generic ; public class SymmetricPairs { static void findSymPairs ( int [ , ] arr ) { Dictionary < int , int > hM = new Dictionary < int , int > ( ) ; int val = 0 ; for ( int i = 0 ; i < arr . GetLength ( 0 ) ; i ++ ) { int first = arr [ i , 0 ] ; int sec = arr [ i , 1 ] ; if ( hM . ContainsKey ( sec ) ) val = hM [ sec ] ; if ( val != 0 && val == first ) Console . WriteLine ( " ( " + sec + " , ▁ " + first + " ) " ) ; else hM . Add ( first , sec ) ; } } public static void Main ( String [ ] arg ) { int [ , ] arr = new int [ 5 , 2 ] ; arr [ 0 , 0 ] = 11 ; arr [ 0 , 1 ] = 20 ; arr [ 1 , 0 ] = 30 ; arr [ 1 , 1 ] = 40 ; arr [ 2 , 0 ] = 5 ; arr [ 2 , 1 ] = 10 ; arr [ 3 , 0 ] = 40 ; arr [ 3 , 1 ] = 30 ; arr [ 4 , 0 ] = 10 ; arr [ 4 , 1 ] = 5 ; findSymPairs ( arr ) ; } }