Find the word with most anagrams in a given 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
function largestAnagramGrp ( arr ) { var 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 , ] ; var max = - 1 ; var maxpdt = - 1 ; var W = { } ; var P = { } ; for ( const temp of arr ) { var c = temp . split ( " " ) ; var pdt = 1 ; for ( const t of c ) { pdt *= prime [ t . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] ; } if ( P . hasOwnProperty ( pdt ) ) { P [ pdt ] = P [ pdt ] + 1 ; } else { W [ pdt ] = temp ; P [ pdt ] = 1 ; } } for ( const [ key , value ] of Object . entries ( P ) ) { if ( max < value ) { max = value ; maxpdt = key ; } } return W [ maxpdt ] ; } var S = " " + " " ; var arr = S . split ( " " ) ; document . write ( largestAnagramGrp ( arr ) ) ;