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...
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...
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.
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 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 →...
In your own words, explain the functions of the following most frequently used unit operations in...
In your own words, explain the functions of the following most frequently used unit operations in chemical engineering processes. (i) dryer, (ii) filter, (iii) distillation columns, (iv) absorber, (v) stripper, (vii) crystallizer, (viii) compressor, (ix) valve, (x) hydrocyclone
Given a string of at least 3 characters as input, if the length of the string...
Given a string of at least 3 characters as input, if the length of the string is odd return the character in the middle as a string. If the string is even return the two characters at the midpoint. -------------------------------------------------------------- public class Class1 { public static String midString(String str) {     //Enter code here } }
public int getIndexOfWord(String[] arr, String word){ // if found, return the index of word, otherwise return...
public int getIndexOfWord(String[] arr, String word){ // if found, return the index of word, otherwise return -1 } public boolean areArrays(int[] arr1, int[] arr2){ // 1. initial check: both arrays need to have the same length, return false if not the same // 2. return true if both given arrats are equals(same values in the same indices), false otherwise } public boolean areArraysEqual(String[] arr1, String[] arr2){ // 1. initial check: both arrays need to have the same length, return false...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT