title: How to remove the repeated consecutive elements in dynamic array in systemverilog?
body: <pre><code>module tb;
    
    bit [7:0] bytes [];
    bit [7:0] q[$];
    
    initial begin
      bytes = new[20];
      bytes[0] = 8'hee;
      bytes[1] = 8'hea;
      bytes[2] = 8'haf;
      bytes[3] = 8'haf;
      bytes[4] = 8'haf;
      bytes[5] = 8'h50;
      bytes[6] = 8'h6a;
      bytes[7] = 8'h0d;
      bytes[8] = 8'hc8;
      bytes[9] = 8'hc8;
      bytes[10] = 8'h21;
      foreach(bytes[i])
        $write(&quot;%h &quot;, bytes[i]);
      $display(&quot;\n&quot;);
      q = bytes.unique();
      foreach(q[i])
        $write(&quot;%h &quot;, q[i]);
    end
  endmodule
  </code></pre>
  <p>In the above code, I need to remove the consecutive duplicate elements (only the consecutive duplicate elements) of the dynamic array called <code>bytes</code> and put them in a queue called <code>q</code>. Also, I need to remove all the trailing 0's. I tried using <code>unique()</code>, but what I get is something different.</p>
  <p>Expected output:</p>
  <pre><code>ee ea af af af 50 6a 0d c8 c8 21 00 00 00 00 00 00 00 00 00 00
  
  ee ea af 50 6a 0d c8 21
  </code></pre>
  <p>Output I am getting on QuestaSim on edaplayground</p>
  <pre><code>ee ea af af af 50 6a 0d c8 c8 21 00 00 00 00 00 00 00 00 00 00 
   
  00 0d 21 50 6a af c8 ea ee
  </code></pre>
  <hr />
  <p>More information:</p>
  <p>Once it's 0, it cannot have more duplicates. Since it's a dynamic array created by reading the data values on DUT interface, last value read is the 0 and that remains at 0 unless new data.</p>
  <p>Since its duplication is only due to the current data being held on the output interface (following some arbitartion priority, waiting for few cycles for next priority port data). and the data is constraint randomized to have unique values (data which is input to DUT).</p>
  <p>eg. I send a,b,c,d on the input on port 3 and port 3 waits for few cycles to check is any other port is requested, hence the output may be something like a bbb cc d ( 3 cycle for b and 2 cycle for c) and the dynamic array reads this output. Removing the duplicated items is so as to compare the input and output equivalence in Scoreboard.</p>