How to safely delete entries from a queue

hi,
you can use following code to delete entries from queue without using an associative array. I have tried this and it works. The code consists of two functions, find_and_delete, which finds out a matching entry in queue and deletes one entry and exits the loop. This function is called number of times equal to the number of matching entries in queue i.e. if there are 9 matching entries, function find_and_delete is called 9 times as below:
You can combine two functions find_and_delete and find_cnt to write a single function which can delete matching entries from queue.


module my_mod();
  
  function int find_and_delete (int x, ref int pq[$]);
    int tmp = 0;
    for (int i = 0; i < mq.size(); i ++) begin
      if (pq[i] == x) begin
        pq.delete (i);
         tmp = 1;
         break;
      end
      /*else begin
          continue;
      end */
    end
    return tmp; 
  endfunction
  
  function int find_cnt (input int x, input int pq[$]); 
    int cnt = 0;
    for ( int i = 0; i < pq.size(); i++) begin
      if (pq[i] == x) begin
        cnt ++;
      end
    end  
    return cnt;
  endfunction
  
  int mq[$] = {1, 2, 2, 2, 3, 2, 4, 2, 2, 6, 2, 8, 2, 2, 4, 5};
  int cnt = 0;
  int tmp1;
  initial  begin
    cnt = find_cnt (2, mq);
    
    for (int i = 0; i < cnt; i++) begin
      tmp1 = find_and_delete (2, mq);
    end
    if (tmp1 == 0) begin
      $display ("no matching entries");
    end
    foreach (mq[i])
      $display (mq[i]);
    mq.delete();
  end
endmodule