title: Redundant code in a loop
body: <p>I have a list:</p>
  
  <pre><code>id num1 ...
  -----------
   1  123 ...
   1  456 ...
   2  789 ...
   2  666 ...
  </code></pre>
  
  <p>And would like to create an object array based on it:</p>
  
  <pre><code>{ 1, [123, 456], [...] },
  { 2, [789, 666], [...] } 
  </code></pre>
  
  <p>Here is my pseudo code:</p>
  
  <pre><code>int previous_id = -1;
  array a1 = null; // B
  array a2 = null; // B
  array a3 = null; // B
  array a4 = null; // B
  while (++c) { // c is a pointer pointing to the list
    if (c.id != previous_id &amp;&amp; previous_id != -1) {
      j[i++].id = previous_id;   // A
      j[i++].data1 = a1;         // A
      j[i++].data2 = a2;         // A
      j[i++].data3 = a3;         // A
      j[i++].data4 = a4;         // A
      a1 = null;   // B
      a2 = null;   // B
      a3 = null;   // B
      a4 = null;   // B
    }
    a1.add(c.num1);
    a2.add(c.num2);
    a3.add(c.num3);
    a4.add(c.num4);
    previous_id = c.id;
  }
  j[i++].id = previous_id;   // A
  j[i++].data1 = a1;         // A
  j[i++].data2 = a2;         // A
  j[i++].data3 = a3;         // A
  j[i++].data4 = a4;         // A
  </code></pre>
  
  <p>It is working but with some redundant code, i.e. A and B</p>
  
  <p>Is it possible to merge them to make it more concise and clear?</p>