Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find out if a value is in a deque?

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?

like image 337
compStudent Avatar asked Feb 04 '15 01:02

compStudent


People also ask

How do you know if a value is in deque?

Deque contains() method in Java Return Value: The method returns True if the element is present in the Deque otherwise it returns False.

How do you find an element in a deque?

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.

Can you index a deque?

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 .

How do you peek in deque?

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.


1 Answers

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
like image 163
Bill Lynch Avatar answered Sep 23 '22 07:09

Bill Lynch