java - Count frequency of words in a given file - Code Review Stack Exchange
a String an integer providing the number of items to return Implement the function such that it returns a list of Strings ordered by word frequency, the most frequently occurring word first. Runtime not to exceed \$O(n)\$ I decided to use a map to count the frequency of the words and then do the count sort to achieve sorting in linear time. Is this the best approach or there is some other solution that I am not aware of? import java.util.Arrays; import java.util.HashMap; import java.util.Map; interface IFrequencyCounter{ public String[] getMostFrequenctWords(String content, int numberOfwords ); } public class FrequencyCounter implements IFrequencyCounter{ /** * This is the Frequency Class * which has only one property count. * This is used to save autoboxing/unboxing * overhead when using maps. */ private class Frequency{ private Frequency(){ this.count = 1; } private int count; public int getFrequency(){ return this.count; } public void incrementFrequency(){ this.count++;Read full article from java - Count frequency of words in a given file - Code Review Stack Exchange
No comments:
Post a Comment