words=['one','two','one','three','one']
word_counts=dict()
#after the loop the dictionary will have each word frequency
for word in words:
try:
word_counts[word]+=1
except:
word_counts[word]=1
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;
}
}