Most frequent word in an array of strings | Java implementation ; Function returns word with highest frequency ; Create HashMap to store word and it 's frequency ; Iterate through array of words ; If word already exist in HashMap then increase it 's count by 1 ; Otherwise add word to HashMap ; Create set to iterate over HashMap ; Check for word having highest frequency ; Return word having highest frequency ; Driver code ; Print word having highest frequency
import java . util . * ; class GKG { static String findWord ( String [ ] arr ) { HashMap < String , Integer > hs = new HashMap < String , Integer > ( ) ; for ( int i = 0 ; i < arr . length ; i ++ ) { if ( hs . containsKey ( arr [ i ] ) ) { hs . put ( arr [ i ] , hs . get ( arr [ i ] ) + 1 ) ; } else { hs . put ( arr [ i ] , 1 ) ; } } Set < Map . Entry < String , Integer > > set = hs . entrySet ( ) ; String key = " " ; int value = 0 ; for ( Map . Entry < String , Integer > me : set ) { if ( me . getValue ( ) > value ) { value = me . getValue ( ) ; key = me . getKey ( ) ; } } 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 ) ; System . out . println ( sol ) ; } }