say i have a deque with values [0,3,5,1,5,8]. I want to save all information about the deque including order, but I have to find if the value 5 is in the deque.
What is some pseudo-code that could determine this?
Deque contains() method in Java Return Value: The method returns True if the element is present in the Deque otherwise it returns False.
How to check/find an item in Dequeue using find() method. find() function finds the element in the given range of numbers. Returns an iterator to the first element in the range [first, last) that compares equal to the value to be searched. If no such element is found, the function returns last.
Then you remove "d" from the deque using . remove() . Deques also allow indexing to access items, which you use here to access "b" at index 1 .
Deque too can be interpreted as a list in terms of accessing using indices. You can peek front element by using deque[0] and peek last using deque[-1] This works without popping elements from left or right and seems efficient too.
Are you aware of the in
operator?
>>> import collections
>>> d = collections.deque([0,3,5,1,5,8])
>>> 5 in d
True
>>> 20 in d
False
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With