Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>#include &lt;iostream&gt; #include &lt;string&gt; #include &lt;vector&gt; #include &lt;map&gt; using namespace std; string most_occurred(vector&lt;string&gt; &amp;vec) { map&lt;string, unsigned long&gt; str_map1; std::string max_element = ""; int max_count = 0; typedef vector&lt;string&gt;::const_iterator iter; iter end = vec.end(); for (iter it = vec.begin(); it != end; ++it) { int count = ++str_map1[*it]; if(count == max_count) max_element = "no max found"; if(count &gt; max_count) { max_count = count; max_element = *it; } } return max_element; } int main() { std::string arr1[5] = {"aa" , "bb", "cc", "dd", "ee"}; // no max found std::string arr2[5] = {"aa" , "aa", "cc", "dd", "ee"};// aa std::string arr3[5] = {"aa" , "aa", "cc", "cc", "ee"}; // no max found std::vector&lt;std::string&gt; input1(arr1, arr1+5); std::vector&lt;std::string&gt; input2(arr2, arr2+5); std::vector&lt;std::string&gt; input3(arr3, arr3+5); std::cout &lt;&lt; most_occurred(input1) &lt;&lt; std::endl; std::cout &lt;&lt; most_occurred(input2) &lt;&lt; std::endl; std::cout &lt;&lt; most_occurred(input3) &lt;&lt; std::endl; } </code></pre> <p>the result is:</p> <pre><code>no max found aa no max found </code></pre> <hr> <p>The following test resulted in <code>no max found</code>.</p> <pre><code>int main() { std::string arr1[24] = {"Element1", "Element2", "Element33", "1", "Element1", "Element2", "Element33", "2", "Element11", "Element2", "Element33", "2", "Element11" "Element21" "Element31", "2", "Element11", "Element21", "Element31", "1", "Element12", "Element21", "Element31", "1"}; // no max found std::vector&lt;std::string&gt; input1(arr1, arr1+24); std::cout &lt;&lt; most_occurred(input1) &lt;&lt; std::endl; } </code></pre> <p>If the above code returns std::string("") then there was no max element, else it will return the max.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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