Most frequent word in an array of strings | C # implementation ; Function returns word with highest frequency ; Create Dictionary to store word and it 's frequency ; Iterate through array of words ; If word already exist in Dictionary then increase it 's count by 1 ; Otherwise add word to Dictionary ; Create set to iterate over Dictionary ; Check for word having highest frequency ; Return word having highest frequency ; Driver code ; Print word having highest frequency
using System ; using System . Collections . Generic ; class GFG { static String findWord ( String [ ] arr ) { Dictionary < String , int > hs = new Dictionary < String , int > ( ) ; for ( int i = 0 ; i < arr . Length ; i ++ ) { if ( hs . ContainsKey ( arr [ i ] ) ) { hs [ arr [ i ] ] = hs [ arr [ i ] ] + 1 ; } else { hs . Add ( arr [ i ] , 1 ) ; } } String key = " " ; int value = 0 ; foreach ( KeyValuePair < String , int > me in hs ) { if ( me . Value > value ) { value = me . Value ; key = me . Key ; } } return key ; } public static void Main ( String [ ] args ) { String [ ] arr = { " geeks " , " for " , " geeks " , " a " , " portal " , " to " , " learn " , " can " , " be " , " computer " , " science " , " zoom " , " yup " , " fire " , " in " , " be " , " data " , " geeks " } ; String sol = findWord ( arr ) ; Console . WriteLine ( sol ) ; } }