Question

In: Computer Science

// For an input string of words, find the most frequently occurring word. In case of...

// For an input string of words, find the most frequently occurring word. In case of any ties, report any in output.
// Your algorithm should be of the runtime O(n) time where n is the number of words in the string.

#include <iostream>
#include <vector>
#include <sstream>

using namespace std;


string findWord(vector<string>& tokens);

int main() {
   string line = "FDo all the good you can, by all the means you can,
                   in all the ways you can, in all the places you can,
                   at all the times you can, to all the people you can,
                   as long as ever you can.";
  

   // Convert string to a vector of words
   char delimiter = ' ';
   string token;
   istringstream tokenStream(line);
   vector<string> tokens;
   while (getline(tokenStream, token, delimiter)) {
       tokens.push_back(token);
   }  
   cout << "The most frequently occuring word is: " << findWord(tokens) << endl;
  
}

string findWord(vector<string>& tokens) {
// Your code here


}

  
  
   // Convert string to a vector of words
   char delimiter = ' ';
   string token;
   istringstream tokenStream(line);
   vector<string> tokens;
   while (getline(tokenStream, token, delimiter)) {
       tokens.push_back(token);
   }  
   cout << "The most frequently occuring word is: " << findWord(tokens) << endl;
  
}

string findWord(vector<string>& tokens) {
// Your code here


}

";
  
  
   // Convert string to a vector of words
   char delimiter = ' ';
   string token;
   istringstream tokenStream(line);
   vector<string> tokens;
   while (getline(tokenStream, token, delimiter)) {
       tokens.push_back(token);
   }  
   cout << "The most frequently occuring word is: " << findWord(tokens) << endl;
  
}

string findWord(vector<string>& tokens) {
// Your code here


}

Solutions

Expert Solution

Here is the code for the above question :

#include <iostream>
#include <vector>
#include <sstream>
#include<unordered_map>

using namespace std;


string findWord(vector<string>& tokens);

int main() {
   string line = "FDo all the good you can, by all the means you can, in all the ways you can, in all the places you can, at all the times you can, to all the people you can, as long as ever you can.";
  

   // Convert string to a vector of words
   char delimiter = ' ';
   string token;
   istringstream tokenStream(line);
   vector<string> tokens;
   while (getline(tokenStream, token, delimiter)) {
       tokens.push_back(token);
   }  
   cout << "The most frequently occuring word is: " << findWord(tokens) << endl;
  
}

string findWord(vector<string>& tokens) {
unordered_map <string, int> m; 
//create a map of words, where each element is [word , frequency of the word]
for(int i = 0 ; i < tokens.size() ; i++){
  if(m.count(tokens[i]) == 0){
    m[tokens[i]] = 1;
  }
  else{
    m[tokens[i]] ++;
  }
}

//now iterate the map and find the word which has the maximum frequency
string freq = "";
int max = INT8_MIN;
for(auto x : m){
  if(x.second > max){
    freq = x.first;
    max = x.second;
  }
}

return freq;


}

Here is the ss and the output for your reference -
Please give a thumbs up if you like the answer. Thank you .


Related Solutions

The mode is which of the following? The most frequently occurring value in a data set...
The mode is which of the following? The most frequently occurring value in a data set The middle most occurring value in a set of values The difference between the highest and lowest values in a set The arithmetic average of a set of values.
Write a method called mode that returns the most frequently occurring element of an array of...
Write a method called mode that returns the most frequently occurring element of an array of integers. Assume that the array has at least one element and that every element in the array has a value between 0 and 100 inclusive. Break ties by choosing the lower value. For example, if the array passed contains the values [27, 15, 15, 11, 27], your method should return 15. write a version of this method that does not rely on the values...
PYTHON: Develop an algorithm for finding the most frequently occurring value in a list of numbers....
PYTHON: Develop an algorithm for finding the most frequently occurring value in a list of numbers. Use a sequence of coins. Place paper clips below each coin that count how many other coins of the same value are in the sequence. Give the pseudocode for an algorithm that yields the correct answer, and describe how using the coins and paper clips helped you find the algorithm. Please solve in python!
5.16 Implement function indexes() that takes as input a word (as a string) and a onecharacter...
5.16 Implement function indexes() that takes as input a word (as a string) and a onecharacter letter (as a string) and returns a list of indexes at which the letter occurs in the word. >>> indexes('mississippi', 's') [2, 3, 5, 6] >>> indexes('mississippi', 'i') [1, 4, 7, 10] >>> indexes('mississippi', 'a') []
Write a Java program that prompts the user to input a word (String). The program must...
Write a Java program that prompts the user to input a word (String). The program must print the reversed word with all consecutive duplicate characters removed. The program must contain the following classes: - The StackX class (you can use the Java Stack class). - The Reverse class which must contain a private data field called “word” of type string, a constructor, and a void method called revNoDup(). The revNoDup() method must reverse the word and remove the consecutive duplicate...
Respond to the following in a minimum of 175 words: The most frequently used measures of...
Respond to the following in a minimum of 175 words: The most frequently used measures of central tendency for quantitative data are the mean and the median. The following table shows civil service examination scores from 24 applicants to law enforcement jobs: 83 74 85 79 82 67 78 70 18 93 64 27 93 98 82 78 68 82 83 99 96 62 93 58 Using Excel, find the mean, standard deviation, and 5-number summary of this sample. Construct...
Write in javaScript: User input 5 words in one input, and print out the longest word.
Write in javaScript: User input 5 words in one input, and print out the longest word.
1. Read a line of input from the user (as a string) and find out if...
1. Read a line of input from the user (as a string) and find out if there are any vowels in the string. Use a break or continue keyword (whichever is appropriate) to notify that a vowel has been found. Prompt for another string and search again until the user enters 'exit' into the program. 2. Ask the user how many random numbers they would like to see. Ask the user to provide the lowest number they would like to...
Write a driver to get a String input from keyboard and if the input string has...
Write a driver to get a String input from keyboard and if the input string has less than 10 characters, throw a StringTooShortException. public class StringTooShortException extends Exception {     //-----------------------------------------------------------------     // Sets up the exception object with a particular message.     //-----------------------------------------------------------------     public StringTooShortException()     {         super("String does not have enough characters");     } }
In this Exercise, you have to take a single string as input. Using this input string,...
In this Exercise, you have to take a single string as input. Using this input string, you have to create multiple queues in which each queue will comprise of separate word appeared in input string. At the end, you will again concatenate all queues to a single queue.s Example: String = “Data Structure and Algo” Q1 = D → a → t → a Q2 = S → t → r → u → c → t → u →...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT