Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

most frequent word in an array of strings python

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
Comment

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;
    }

}
Comment

PREVIOUS NEXT
Code Example
Python :: python move cursor to previous line 
Python :: install coverage python 
Python :: python face recognition 
Python :: access first element of dictionary python 
Python :: [0] * 10 python 
Python :: queue python 
Python :: python search first occurrence in string 
Python :: # extract an email ID from the text using regex 
Python :: how to get the value out of a dictionary python3 
Python :: throw error in python 
Python :: nested loop in list comprehension 
Python :: pyspark now 
Python :: matplotlib vertical line 
Python :: how to change index date format pandas 
Python :: pytorch transpose 
Python :: numpy aray map values with dictionary 
Python :: round off to two decimal places python 
Python :: uninstall python using powershell 
Python :: check regex in python 
Python :: _ variable in python 
Python :: check if element in list python 
Python :: python string startswith regex 
Python :: import python script from another directory 
Python :: make the first letter of a string upper case 
Python :: word2number python 
Python :: adding proxy in selenium python 
Python :: numpy moving average 
Python :: compute condition number python 
Python :: rename row pandas 
Python :: how to concatenate a string with int in python 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =