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 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++ 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;
}
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);
}
}
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()
// 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
Output:
Found
Example 2: Below is a C++ program to demonstrate how to find elements in dequeue.
// 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;
}
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);
}
}
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)
// 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++ 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;
}
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);
}
}
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)
// 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