Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm not sure about an approach with hash-maps or the like, but I whipped up a program which essentially is the brute force way of doing things. Basically you need to keep track of each string and how many types it appears using a dynamic array. Once you have gone through your input and analyzed each string and how many times it appears, you go through your dynamic array and see which string appears the most. Then you simply output it.</p> <p>Try and do this yourself without the help of my program. If you cannot or if you get stuck just refer to the working program below which does what you ask:</p> <pre><code>#include &lt;vector&gt; #include &lt;string&gt; #include &lt;iostream&gt; using namespace std; //This struct represents a string and how many times it appears struct strRefCount { //String and Reference Count unsigned int count; string str; }; strRefCount strMode(string data) //String mode: returns the string which appears most often { vector&lt;strRefCount&gt; allStrings; //Count of each time a string appears and what the string is string curString = ""; //The string we are currently reading (initialize to be empty) unsigned int strPos = 0; //The position (in the form of data[strPos]) which represents how far we have gotten in analyzing the string strRefCount *modeStringp; //Pointer to the string that appears most often while(data[strPos] != NULL) { //We will advance through data until we hit the null terminator in this loop curString.clear(); while(data[strPos] != ' ' &amp;&amp; data[strPos] != NULL) //Advance in the string until we hit a space or terminating null byte { curString += data[strPos]; //Append the string strPos++; //Advance one byte in data } bool flagStringFound = false; //This flag indicates that the string was already found before for(unsigned int i = 0; i &lt; allStrings.size(); i++) { if(allStrings[i].str == curString) //If this string is the same as the current entry { allStrings[i].count++; flagStringFound = true; break; } } if(flagStringFound == false) //If the string is not present in allStrings, put it there and initialize it { strRefCount addElem; //Element to add to the end of the vector addElem.str = curString; //Last element's string is curString addElem.count = 1; //Last element's reference count is curString allStrings.push_back(addElem); //Add the element } //Make sure we don't repeat the loop if we are at the end of the string if(data[strPos] != NULL) { break; } } //Now we have every string which appears in data and the number of times it appears //Go on to produce the correct output modeStringp = &amp;(allStrings[0]); //Set modeStringp to the first string for(unsigned int i = 1; i &lt; allStrings.size(); i++) //Note that by setting i to 1 we skip the first element which is already in modeStringp { if(allStrings[i].count &gt; modeStringp-&gt;count) //If the current entry in allStrings is bigger than { modeStringp = &amp;(allStrings[i]); //Replace modeStringp with the current entry in allStrings } } return *modeStringp; } int main() { string data; getline(cin, data); //Get the input (can't use cin as it doesn't allow for an entire line just space seperated string) strRefCount dataModeString = strMode(data); //Call out strMode function cout &lt;&lt; endl &lt;&lt; dataModeString.str &lt;&lt; " appears most often with a total of " &lt;&lt; dataModeString.count &lt;&lt; " appearances."; getchar(); //This line is only here to make sure we don't quit before we see the output. return 0; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload