title: Finding the most frequent number(s) in a c++ vector
body: <p>New guy here, and a rookie programmer. Can anyone give me an idea how to finish up my program task I gotta do?</p>
  
  <p>So first the user has to input the length of the vector, afterwards input the elements themselves (elements can be only between 0 and 9). </p>
  
  <p>Then the console has to print out the most frequent number and the number of times it occurs. But in case of multiple numbers with the same maximal frequency, it has to print all of them, ordered from smallest to
  largest.</p>
  
  <p>So far, so good, I've managed to do it for an input like this:</p>
  
  <p>"13</p>
  
  <p>4 1 1 4 2 3 4 4 1 2 4 9 3"</p>
  
  <p>And have an output that says that 4 is the most frequent, occurring 5 times.</p>
  
  <p>But for:</p>
  
  <p>"11</p>
  
  <p>7 7 7 0 2 2 2 0 9 9 9"</p>
  
  <p>I sadly hit a wall and cannot think of a simple way to tackle this. </p>
  
  <p>Thank you in advance to whoever decides to read and lend a hand!</p>
  
  <blockquote>
    <p>Blockquote</p>
  </blockquote>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;algorithm&gt; 
  #include &lt;vector&gt;
  
  int main()
  {
  int i = 0;
  int length;
  int elements;
  
  std::cout &lt;&lt; "Enter length and elements of array: ";
  std::cin &gt;&gt; length;
  if (length &lt; 0) { std::cout &lt;&lt; "Don't be naughty now!"; return 0; }
  
  std::vector&lt;int&gt; arr;
  arr.reserve(length);
  for (int i = 0; i&lt;length; i++)
  {
      std::cin &gt;&gt; elements;
      if (elements &gt; 9 || elements &lt; 0)
      {
          std::cout &lt;&lt; "Input can be only between 0 and 9.";
      }
      else
      {
          arr.push_back(elements);
      }
  }
  std::sort(arr.begin(), arr.end());
  int counter = 0;
  int element = 0;
  // std::vector&lt;int&gt; mostFreqNums;
  for (auto i : arr)
  {
      int tempElement = i;
      int tempCount = 0;
      for (auto j : arr)
      {
          if (j == tempElement)
          {
              tempCount++;
              if (tempCount &gt;= counter)
              {
                  element = tempElement;
                  counter = tempCount;
              }
          } 
      }
  }
  //for (auto p : mostFreqNums)
  //{
      std::cout &lt;&lt; "The number " &lt;&lt; element &lt;&lt; " is the most frequent (occurs " &lt;&lt; counter &lt;&lt; " times).\n";
  //}
  
  return 0;
  </code></pre>
  
  <p>}</p>
  
  <blockquote>
    <p>Blockquote</p>
  </blockquote>