You are given a list of lowercase words. Find how many words show up in the list more than once. code example

Example 1: how to find occurrence of words in string

HOW TO COUNT OCCURRENCE OF THE WORDS IN STRING 
(It can also be solved by using nested for loops...)

String str = "I am happy and why not and why are you not happy you should be";
   String [] arr = str.split(" ");
   Map<String, Integer> map = new HashMap<>();

   for (int i=0 ; i < arr.length ; i++){
       if (!map.containsKey(arr[i])){
            map.put(arr[i],1);
           } else{
         map.put(arr[i],map.get(arr[i])+1);
        }
        }
     for(Map.Entry<String, Integer> each : map.entrySet()){

    System.out.println(each.getKey()+" occures " + each.getValue() + " times");
    }

Example 2: calculate term frequency python

from collections import Counter

# Counter token frequency from a sentence
sentence = "Texas A&M University is located in Texas"

term_frequencies = Counter(sentence.split())

Tags:

Java Example