import java.util.Hashtable;


public class MostFrequent {
	public static void main(String[] args) {
		MostFrequent freq = new MostFrequent();
		String word = freq.findMostFrequentWord("This is a sample sentence with various words with various frequencies which will need to be counted so as to determine the word that occurs most often");
		System.out.println("Most frequent word is \"" + word + "\"");
	}
	
	public String findMostFrequentWord(String words) {
		Hashtable<String, Integer> table = new Hashtable<String, Integer>();
		String[] collection = words.split(" ");
		
		int maxFreq = 0;
		String wordWithMaxFreq = null;
		
		for (int i = 0; i < collection.length; i++) {
			Integer result = table.get(collection[i]);
			if (result == null) {
				table.put(collection[i], 1);
			} else {
				int newFreq = result.intValue() + 1;
				table.put(collection[i], newFreq);
				if (newFreq > maxFreq) {
					maxFreq = newFreq;
					wordWithMaxFreq = collection[i];
				}
			}
		}
		
		return wordWithMaxFreq;
	}
}

