Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

most frequent word in an array of strings



class Solution
{
    //Function to find most frequent word in an array of strings.
    public String mostFrequentWord(String arr[],int n)
    {
        
        String ans = "";
       
     HashMap<String, Integer> mp = new HashMap<>();
       
       for(int i=n-1; i >= 0; i--){
          String s = arr[i];
          mp.put(s, mp.getOrDefault(s, 0) + 1);
          if(mp.get(s) > mp.getOrDefault(ans, 0)){
              ans = s;
          }
       }
       
       return ans;
    }

}
 
PREVIOUS NEXT
Tagged: #frequent #word #array #strings
ADD COMMENT
Topic
Name
6+5 =