title: How to refactor this simple code to avoid code duplication?
body: <p>I am solving the following simple problem(on one of OnlineJugde sites which is in Russian, so I won't give a link here:). It is easier to state the problem via an example than definition.</p>
  
  <p><em><strong>Input:</em></strong> </p>
  
  <pre><code>10 // this is N, the number of the integers to follow
  1 1 1 2 2 3 3 1 4 4
  </code></pre>
  
  <p><em><strong>Output:</em></strong></p>
  
  <pre><code>3 times 1.
  2 times 2.
  2 times 3.
  1 times 1.
  2 times 4.    
  </code></pre>
  
  <p><strong><em>Constraints:</em></strong>
  All the numbers in the input(including <strong>N</strong>) are positive integer less than <strong>10000</strong>. </p>
  
  <p>Here is the code I got <strong><em>Accepted</em></strong> with:</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  int main()
  {
     int n; 
     cin &gt;&gt; n;
     int prevNumber = -1;
     int currentCount = 0;
     int currentNumber;
     while(n --&gt; 0)  // do n times
     {
        cin &gt;&gt; currentNumber;
        if(currentNumber != prevNumber)
        {
           if(currentCount != 0) //we don't print this first time
           {
              cout &lt;&lt; currentCount &lt;&lt; " times " &lt;&lt; prevNumber &lt;&lt; "." &lt;&lt; endl;
           }
           prevNumber = currentNumber;
           currentCount = 1;
        }
        else //if(currentNumber == prevNumber)
        {
           ++currentCount;
        }
     }
     cout &lt;&lt; currentCount &lt;&lt; " times " &lt;&lt; prevNumber &lt;&lt; "." &lt;&lt; endl;
  }
  </code></pre>
  
  <p>Now here's my problem. A little voice inside me keeps telling me that I am doing this line two times:</p>
  
  <pre><code>cout &lt;&lt; currentCount &lt;&lt; " times " &lt;&lt; prevNumber &lt;&lt; "." &lt;&lt; endl;
  </code></pre>
  
  <p>I told that voice inside me that it might be possible to avoid printing separately in the end. It told me that there would then be perhaps way too many <code>if</code>'s and <code>else</code>'s for such a simple problem. Now, I <strong><em>don't</em></strong> want to make the code <strong><em>shorter</em></strong>. Nor do I want do minimize the number of <code>if</code>'s and <code>else</code>'s. <em>But I <strong>do</strong> want to get rid of the special printing in the end of the loop without making the code more complicated.</em> </p>
  
  <p>I really believe this simple problem can be solved with simpler code than mine is. Hope I was clear and the question won't be deemed as not constructive :)</p>
  
  <p>Thanks in advance.</p>