# Delete a particular item in a queue without using delete method

**URL:** <https://verificationacademy.com/forums/t/delete-a-particular-item-in-a-queue-without-using-delete-method/39369>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, queues-systemverilog\
**Created:** [July 26, 2021, 6:19pm UTC](https://verificationacademy.com/forums/t/delete-a-particular-item-in-a-queue-without-using-delete-method/39369 "2021-07-26T18:19:23Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![verific\_engi](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/v/c5a1d2/32.png) [@verific\_engi](https://verificationacademy.com/forums/u/verific_engi)\
**Post date:** [July 26, 2021, 6:19pm UTC](https://verificationacademy.com/forums/t/delete-a-particular-item-in-a-queue-without-using-delete-method/39369/1 "2021-07-26T18:19:23Z")

</div>

q[$] = {12,3,45,23};

How to delete 45 from the queue without using delete or pop\_back methods to pop front or back items

---

<div class="post-metadata">

**Author:** ![KillSteal](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/k/74df32/32.png) [@KillSteal](https://verificationacademy.com/forums/u/KillSteal)\
**Post date:** [July 26, 2021, 6:42pm UTC](https://verificationacademy.com/forums/t/delete-a-particular-item-in-a-queue-without-using-delete-method/39369/2 "2021-07-26T18:42:39Z")

</div>

_In reply to [verific\_engi](https://verificationacademy.com/forums/t/delete-a-particular-item-in-a-queue-without-using-delete-method/39369):_

If this is an interview question, the interviewer is trying to understand how you think and approach this question. Note that it;s highly unlikely that you would want to implement the above in a real practical use case. It’s just a tricky question for interviews.

Start your answer with : Queues are good at deleting/adding at the beginning or at the end. If you want to delete elements from the middle, Queues are in-efficient, and you may want to consider other data structures. This answer establishes that you have given the questions more high level thought.

However, if you still want to achieve the above :

As unpacked array methods can be used on queues, you can find the index at which 45 is present, and concatenate two queues skipping 45. Something like below :

```verilog

module test();
  int q[$] = {12,3,45,23};
  int b[];
  initial begin
    b = q.find_first_index(x) with (x==45);
    q = {q[0:b[0]-1],q[b[0]+1:$]};
  end
endmodule

```

With the above idea, you will have to take care of corner cases like when the element is the first or the last in a queue.

---

<div class="post-metadata">

**Author:** ![rlraj\_2020](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/df705f/32.png) [@rlraj\_2020](https://verificationacademy.com/forums/u/rlraj_2020)\
**Post date:** [February 1, 2023, 10:12pm UTC](https://verificationacademy.com/forums/t/delete-a-particular-item-in-a-queue-without-using-delete-method/39369/3 "2023-02-01T22:12:21Z")

</div>

_In reply to [KillSteal](https://verificationacademy.com/forums/t/delete-a-particular-item-in-a-queue-without-using-delete-method/39369/2):_

why do you say queues are inefficient to delete elements in the middle of the queue ?

besides static or dynamic array, both queues and associative arrays allow us to delete an item based on the index using the delete(index) method.

please can you elaborate more on other efficient data structures besides queues ?

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [February 1, 2023, 10:46pm UTC](https://verificationacademy.com/forums/t/delete-a-particular-item-in-a-queue-without-using-delete-method/39369/4 "2023-02-01T22:46:51Z")

</div>

_In reply to [rlraj\_2020](https://verificationacademy.com/forums/t/delete-a-particular-item-in-a-queue-without-using-delete-method/39369/3):_

I don’t agree with that explanation. Queues are the best kind of array for adding or deleting one element at a time. While it might be true that accessing (reading or writing) an element in the middle queue might be less efficient than other array kinds, having to reconstruct the entire queue because an element gets added or deleted is going to be less efficient. It would be much better to write this as

```verilog
module test();
  int q[$] = {12,3,45,23};
  int b[$]; // all 'find' methods return a queue in case nothing is found.
  initial begin
    b = q.find_first_index(x) with (x==45);
    if (b.size() == 1) q.delete(b[0]);
  end
endmodule

```

---

<div class="post-metadata">

**Author:** ![rlraj\_2020](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/df705f/32.png) [@rlraj\_2020](https://verificationacademy.com/forums/u/rlraj_2020)\
**Post date:** [February 2, 2023, 5:15pm UTC](https://verificationacademy.com/forums/t/delete-a-particular-item-in-a-queue-without-using-delete-method/39369/5 "2023-02-02T17:15:45Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/delete-a-particular-item-in-a-queue-without-using-delete-method/39369/4):_

Agree. But the dude at the top was asking for an interview answer without using the delete() method. So KillSteal’s solution was good but I was just curious about his statement that says

> If you want to delete elements from the middle, Queues are in-efficient, and you may want to consider other data structures

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [February 2, 2023, 5:25pm UTC](https://verificationacademy.com/forums/t/delete-a-particular-item-in-a-queue-without-using-delete-method/39369/6 "2023-02-02T17:25:53Z")

</div>

_In reply to [rlraj\_2020](https://verificationacademy.com/forums/t/delete-a-particular-item-in-a-queue-without-using-delete-method/39369/5):_

> _In reply to [dave\_59](https://verificationacademy.com/forums/t/delete-a-particular-item-in-a-queue-without-using-delete-method/39369/5):_  
> Agree. But the dude at the top was asking for an interview answer without using the delete() method. So KillSteal’s solution was good but I was just curious about his statement that says

The statement you quoted is what I disagree with. Queues are the most effecent kind of array for deleting a single element, regardless of whether they are in the middle or not.

---

<div class="post-metadata">

**Author:** ![Radha9](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/c5a1d2/32.png) [@Radha9](https://verificationacademy.com/forums/u/Radha9)\
**Post date:** [July 4, 2023, 12:33am UTC](https://verificationacademy.com/forums/t/delete-a-particular-item-in-a-queue-without-using-delete-method/39369/7 "2023-07-04T00:33:18Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/delete-a-particular-item-in-a-queue-without-using-delete-method/39369/6):_

> _In reply to [rlraj\_2020](https://verificationacademy.com/forums/t/delete-a-particular-item-in-a-queue-without-using-delete-method/39369/6):_  
> I don’t agree with that explanation. Queues are the best kind of array for adding or deleting one element at a time. While it might be true that accessing (reading or writing) an element in the middle queue might be less efficient than other array kinds, having to reconstruct the entire queue because an element gets added or deleted is going to be less efficient. It would be much better to write this as

```verilog
module test();
int q[$] = {12,3,45,23};
int b[$]; // all 'find' methods return a queue in case nothing is found.
initial begin
b = q.find_first_index(x) with (x==45);
if (b.size() == 1) q.delete(b[0]);
end
endmodule

```

Thanks for the explanation, but here the delete method is used which is not expected.

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [July 4, 2023, 2:08am UTC](https://verificationacademy.com/forums/t/delete-a-particular-item-in-a-queue-without-using-delete-method/39369/8 "2023-07-04T02:08:25Z")

</div>

_In reply to [Radha9](https://verificationacademy.com/forums/t/delete-a-particular-item-in-a-queue-without-using-delete-method/39369/7):_

This is an old and long conversation thread. Probably didn’t make this clear enough. I was commenting on the explanation given by KillSteal, not the concatenation example code. which is one way of answering the original question.

My code was showing the most efficient way to delete one element(45) from the queue.
