Reputation: 46147
What is the most optimal way (algorithm) to search for the word that has the maximum number of occurrences in a document?
Upvotes: 2
Views: 2367
Reputation: 178511
Finding the word that occures most times in a document can be done in O(n) by a simple histogram [hash based]:
histogram <- new map<String,int>
for each word in document:
if word in histogram:
histogram[word] <- histogram[word] + 1
else:
histogram[word] <- 1
max <- 0
maxWord<- ""
for each word in histogram:
if histogram[word] > max:
max <- histogram[word]
maxWord <- word
return maxWord
This is O(n) solution, and since the problem is clearly Omega(n) problem, it is optimal in terms of big O notation.
Upvotes: 2
Reputation: 500873
Upvotes: 2