Common characters in n strings | C # Program to find all the common characters in n strings ; primary array for common characters we assume all characters are seen before . ; for each string ; secondary array for common characters Initially marked false ; for every character of ith string ; if character is present in all strings before , mark it . ; Copy whole secondary array into primary ; Displaying common characters ; Driver code
using System ; class GFG { static int MAX_CHAR = 26 ; public static void commonCharacters ( String [ ] str , int n ) { Boolean [ ] prim = new Boolean [ MAX_CHAR ] ; for ( int i = 0 ; i < prim . Length ; i ++ ) prim [ i ] = true ; for ( int i = 0 ; i < n ; i ++ ) { Boolean [ ] sec = new Boolean [ MAX_CHAR ] ; for ( int s = 0 ; s < sec . Length ; s ++ ) sec [ s ] = false ; for ( int j = 0 ; j < str [ i ] . Length ; j ++ ) { if ( prim [ str [ i ] [ j ] - ' a ' ] ) sec [ str [ i ] [ j ] - ' a ' ] = true ; } Array . Copy ( sec , 0 , prim , 0 , MAX_CHAR ) ; } for ( int i = 0 ; i < 26 ; i ++ ) if ( prim [ i ] ) { Console . Write ( ( char ) ( i + 97 ) ) ; Console . Write ( " ▁ " ) ; } } public static void Main ( String [ ] args ) { String [ ] str = { " geeksforgeeks " , " gemkstones " , " acknowledges " , " aguelikes " } ; int n = str . Length ; commonCharacters ( str , n ) ; } }