How to check/find an item in Dequeue using find() method
Last Updated :
30 Apr, 2024
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.
Syntax:
InputIterator find (InputIterator first, InputIterator last, const T& val)
Parameters:
first, last : Input iterators to the initial and final positions in a sequence.The range searched is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
val: Value to be search in the range.
Return Value:
An iterator to the first element in the range that compares equal to val.
If no elements match, the function returns last.
Example:
Input: 10 20 30 40
Output: Element 30 found at position : 2 (counting from zero)
Input: 8 5 9 2 7 1 3 10
Output: Element 4 not found.
Example 1: Below is the program to implement find() function in deque.
C++
// C++ program to implement
// the above approach
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
// Function to find element
// in a deque
void find(deque<int> q)
{
deque<int>::iterator itr;
itr = find(q.begin(), q.end(), 2);
if(itr != q.end())
{
cout << "Found";
}
else
{
cout << "Not Found";
}
}
// Driver code
int main()
{
// Declaring a deque
deque<int> q;
// Initializing deque
q.push_back(1);
q.push_back(2);
q.push_back(3);
q.push_back(4);
q.push_back(5);
// Calling function find()
find(q);
return 0;
}
Java
import java.util.Deque;
import java.util.ArrayDeque;
public class Main {
// Function to find element in a deque
static void find(Deque<Integer> q, int element) {
// Check if the element is present in the deque
if (q.contains(element)) {
System.out.println("Found");
} else {
System.out.println("Not Found");
}
}
// Driver code
public static void main(String[] args) {
// Declaring and initializing a deque
Deque<Integer> q = new ArrayDeque<>();
// Adding elements to the deque
q.add(1);
q.add(2);
q.add(3);
q.add(4);
q.add(5);
// Element to be found
int elementToFind = 2;
// Calling the find function
find(q, elementToFind);
}
}
Python3
from collections import deque
# Function to find element in a deque
def find(q):
if 2 in q:
print("Found")
else:
print("Not Found")
# Driver code
def main():
# Declaring a deque
q = deque()
# Initializing deque
q.append(1)
q.append(2)
q.append(3)
q.append(4)
q.append(5)
# Calling function find()
find(q)
if __name__ == "__main__":
main()
JavaScript
// Define a class for deque
class Deque {
constructor() {
this.items = [];
}
// Function to add element to the front of deque
appendleft(element) {
this.items.unshift(element);
}
// Function to add element to the rear of deque
append(element) {
this.items.push(element);
}
// Function to check if element is in the deque
contains(element) {
return this.items.includes(element);
}
}
// Function to find element in a deque
function find(q) {
// Check if element 2 is in the deque
if (q.contains(2)) {
console.log("Found");
} else {
console.log("Not Found");
}
}
// Driver code
function main() {
// Declaring a deque
const q = new Deque();
// Initializing deque
q.appendleft(1);
q.append(2);
q.append(3);
q.append(4);
q.append(5);
// Calling function find()
find(q);
}
// Call the main function
main();
Output:
Found
Example 2: Below is a C++ program to demonstrate how to find elements in dequeue.
C++
// C++ program to implement
// the above approach
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
// Function to find an element
// in a deque
void find(deque<string> q)
{
deque<string>::iterator itr;
itr = find(q.begin(), q.end(),
"Raj");
if(itr != q.end())
{
cout << "Found";
}
else
{
cout << "Not Found";
}
}
// Driver code
int main()
{
// Declaring a deque
deque<string> q;
// Initializing a deque
q.push_back("Akshit");
q.push_back("Nikita");
q.push_back("Deeksha");
q.push_back("Nandish");
q.push_back("Rajat");
// Calling find() function
find(q);
return 0;
}
Java
import java.util.ArrayDeque;
import java.util.Deque;
public class Main {
// Function to find an element in a deque
static void find(Deque<String> q) {
if (q.contains("Raj")) {
System.out.println("Found");
} else {
System.out.println("Not Found");
}
}
// Driver code
public static void main(String[] args) {
// Declaring a deque
Deque<String> q = new ArrayDeque<>();
// Initializing a deque
q.add("Akshit");
q.add("Nikita");
q.add("Deeksha");
q.add("Nandish");
q.add("Rajat");
// Calling find() function
find(q);
}
}
Python3
from collections import deque
# Function to find an element
# in a deque
def find(q):
if "Raj" in q:
print("Found")
else:
print("Not Found")
# Driver code
if __name__ == "__main__":
# Declaring a deque
q = deque()
# Initializing a deque
q.append("Akshit")
q.append("Nikita")
q.append("Deeksha")
q.append("Nandish")
q.append("Rajat")
# Calling find() function
find(q)
JavaScript
// Class implementation of a deque
class Deque {
constructor() {
this.q = [];
}
// Function to add an element to the front of the deque
push_back(element) {
this.q.push(element);
}
// Function to add an element to the back of the deque
push_front(element) {
this.q.unshift(element);
}
// Function to remove and return the front element of the deque
pop_front() {
return this.q.shift();
}
// Function to remove and return the back element of the deque
pop_back() {
return this.q.pop();
}
// Function to find an element in the deque
find(target) {
return this.q.includes(target) ? "Found" : "Not Found";
}
}
// Driver code
function main() {
// Declaring a deque
const q = new Deque();
// Initializing a deque
q.push_back("Akshit");
q.push_back("Nikita");
q.push_back("Deeksha");
q.push_back("Nandish");
q.push_back("Rajat");
// Calling find() function
console.log(q.find("Raj"));
}
// Execute the main function
main();
Output:
Not Found
std::find() in C++ vs find in Deque
In std::find() in C++, the range searched is [first, last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
In the case of finding an item in deque using find() function, the range searched is [first, last] i.e both first and last inclusive.
Below is the C++ program to demonstrate finding an item in dequeue.
C++
// C++ program to implement
// the above approach
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
void find(deque<int> q)
{
deque<int>::iterator itr;
itr = find(q.begin(),
q.end(), 5);
if(itr != q.end())
{
cout << "Found";
}
else
{
cout << "Not Found";
}
}
// Driver code
int main()
{
// Declaring a deque
deque<int> q;
// Initializing deque
q.push_back(1);
q.push_back(2);
q.push_back(3);
q.push_back(4);
q.push_back(5);
// Calling find() function
find(q);
return 0;
}
Java
import java.util.Deque;
import java.util.ArrayDeque;
class Main {
static void find(Deque<Integer> q) {
if (q.contains(5)) {
System.out.println("Found");
} else {
System.out.println("Not Found");
}
}
public static void main(String[] args) {
// Declaring a deque
Deque<Integer> q = new ArrayDeque<>();
// Initializing deque
q.add(1);
q.add(2);
q.add(3);
q.add(4);
q.add(5);
// Calling find() function
find(q);
}
}
Python3
from collections import deque
def find(q):
if 5 in q:
print("Found")
else:
print("Not Found")
# Declaring a deque
q = deque()
# Initializing deque
q.append(1)
q.append(2)
q.append(3)
q.append(4)
q.append(5)
# Calling find() function
find(q)
JavaScript
// Function to find the element in the deque
function find(q) {
// Using Array.prototype.includes() to check if the element exists
if (q.includes(5)) {
console.log("Found");
} else {
console.log("Not Found");
}
}
// Driver code
function main() {
// Declaring a deque
let q = [];
// Initializing deque
q.push(1);
q.push(2);
q.push(3);
q.push(4);
q.push(5);
// Calling find() function
find(q);
}
// Call the main function
main();
Output:
Found
Similar Reads
Deque in C++ STL In C++, deque container provides fast insertion and deletion at both ends. Stands for Double Ended QUEue, it is a special type of queue where insertion and deletion operations are possible at both the ends in constant time complexity.Example:C++#include <iostream> #include <deque> using
6 min read
Commonly Used Methods
deque::push_front() in C++ STLDeque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may
2 min read
deque::push_back() in C++ STLDeque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may
2 min read
deque::pop_front() and deque::pop_back() in C++ STLDeque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may
4 min read
Deque::front() and deque::back() in C++ STLDeque or Double Ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also at the beginning. Unlike vectors, contiguous storage allocation may
4 min read
deque insert() function in C++ STLThe deque::insert() function is a built-in function in C++ which is used to insert elements in the deque. The insert() function can be used in three ways: Extends deque by inserting a new element val at a position.Extends deque by inserting n new element of value val in the deque.Extends deque by in
3 min read
deque::begin() and deque::end in C++ STLDeque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may
3 min read
Deque::empty() and deque::size() in C++ STLDeque or Double Ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also at the beginning. Unlike vectors, contiguous storage allocation may
4 min read
deque::clear() and deque::erase() in C++ STLDeque or Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also at the beginning. Unlike vectors, contiguous storage allocation
5 min read
Other Member Methods
deque max_size() function in C++ STLThe deque::max_size() is a built-in function in C++ STL which returns the maximum number of elements that a deque container can hold. Syntax: deque_name.max_size()Parameters: The function does not accept any parameters. Return Value: The function returns the maximum number of elements that a deque c
1 min read
deque assign() function in C++ STLThe deque::assign() is a built-in function in C++ STL which is used to assign values to the same or different deque container. On being called more than once in the same program, the function destroys the values of the previous elements and re-assigns new set of elements to the container. Syntax: de
2 min read
deque rbegin() function in C++ STLThe deque::rbegin() is an inbuilt function in C++ STL which returns a reverse iterator which points to the last element of the deque (i.e., its reverse beginning). Syntax: deque_name.rbegin()Parameter: This function does not accept any parameters. Return value: It returns a reverse iterator which po
2 min read
deque rend() function in C++ STLThe deque::rend() is an inbuilt function in C++ STL which returns a reverse iterator which points to the position before the beginning of the deque (which is considered its reverse end). Syntax: deque_name.rend()Parameter: This function does not accept any parameters. Return value: It returns a reve
2 min read
deque cbegin() in C++ STLThe cbegin() method in deque is a function in C++ STL which returns an iterator pointing to the first element of the container. Syntax: deque_name.cbegin() Return value: It returns a constant iterator pointing to the first element of the deque. This means, that the iterator can be used to traverse t
2 min read
deque cbegin() in C++ STLThe cbegin() method in deque is a function in C++ STL which returns an iterator pointing to the first element of the container. Syntax: deque_name.cbegin() Return value: It returns a constant iterator pointing to the first element of the deque. This means, that the iterator can be used to traverse t
2 min read
deque::operator= and deque::operator[] in C++ STLDeque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may
4 min read
deque::at() and deque::swap() in C++ STLDeque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may
4 min read
How Deque Works Internally in C++? Prerequisite: Deque in C++ Deque or Double Ended Queue is a generalized version of the Queue data structure that allows insert and deletion at both ends. It supports the access of elements in O(1) time complexity and the insertion and deletion of elements from front and back are both done with O(1)
4 min read
Deque of Pairs in C++ with Examples What is a deque? In C++, the deque is a sequence container and it is also known by the name, double-ended queue. As the name implies, a deque allows insertion and deletion from both ends. Although a deque is similar to a vector, deques are more efficient compared to vectors. In vectors, contiguous s
5 min read
Difference between Queue and Deque in C++ Queue: A Queue is a linear data structure that follows a First In First Out (FIFO) order in which the operations are performed. It is a type of container adaptor where elements are inserted into one end of the container and deleted from the other. Functions: empty(): Tests whether the queue is empty
4 min read
Deque vs Vector in C++ STL Deque in C++ Standard Template Library (STL) Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors but support inserting and deleting the first element in O(1). Unlike vectors, contiguous storage allocation is not guarante
2 min read
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. Syntax: InputIterator find (InputIterator first, InputIterator
6 min read