Find the word with most anagrams in a given sentence | C # program to find the word with most anagrams in a sentence ; Function to find the word with maximum number of anagrams ; Primes assigned to 26 alphabets ; Stores the product and word mappings ; Stores the frequencies of products ; Calculate the product of primes assigned ; If product already exists ; Otherwise ; Fetch the most frequent product ; Return a string with that product ; Driver Code
using System ; using System . Collections . Generic ; class GFG { private static String largestAnagramGrp ( String [ ] arr ) { int [ ] prime = { 2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 , 41 , 43 , 47 , 53 , 59 , 61 , 67 , 71 , 73 , 79 , 83 , 89 , 97 , 101 } ; int max = - 1 ; long maxpdt = - 1 ; Dictionary < long , String > W = new Dictionary < long , String > ( ) ; Dictionary < long , int > P = new Dictionary < long , int > ( ) ; foreach ( String temp in arr ) { char [ ] c = temp . ToCharArray ( ) ; long pdt = 1 ; foreach ( char t in c ) { pdt *= prime [ t - ' a ' ] ; } if ( P . ContainsKey ( pdt ) ) { P [ pdt ] = P [ pdt ] + 1 ; } else { W . Add ( pdt , temp ) ; P . Add ( pdt , 1 ) ; } } foreach ( KeyValuePair < long , int > e in P ) { if ( max < e . Value ) { max = e . Value ; maxpdt = e . Key ; } } return W [ maxpdt ] ; } public static void Main ( String [ ] args ) { String S = " please ▁ be ▁ silent ▁ and ▁ listen " + " ▁ to ▁ what ▁ the ▁ professor ▁ says ▁ " ; String [ ] arr = S . Split ( ' ▁ ' ) ; Console . WriteLine ( largestAnagramGrp ( arr ) ) ; } }