How to Check if a Vector Contains a Given Element in C++?
Last Updated :
03 Oct, 2024
In C++, a vector is a dynamic array that provides a resizable and efficient way to store elements. Vectors store their elements in contiguous memory locations, similar to arrays, which allows for efficient access and iteration.
In this article, we will learn the different methods to check whether the vector contains the given element or not in C++.
Example:
Input: vec = {1, 7, 2, 9, 1, 6} , target = 9
Output: Found
Input: vec = {1, 7, 2, 9, 1, 6} , target = 3
Output: Not Found
There are various methods to check whether the vector contain the given element or not in C++.
Using std::count()
To check whether an element is present in the vector or not, we can use the std::count() method that counts the number of occurrences of a value in the given range. If the count returns zero, it means that the element is not present. If it returns non-zero value, it means that the value is present in the vector.
Code Implementation
C++
// C++ program to check whether the given value
// is present in the vector or not using std::count()
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> vec = {1, 7, 2, 9, 1, 6};
int target = 9;
// Count the occurrences of the target value in the
// vector
int cnt = count(vec.begin(), vec.end(), target);
// Check if the target value was found
if (cnt > 0)
cout << "Found";
else
cout << "Not Found";
return 0;
}
Time Complexity: O(n), where n is the number of elements in the vector.
Space Complexity: O(1)
Using std::find()
We can also use the std::find() function for checking an element present in the vector or not. This function returns an iterator to the first occurrence of the given target element in the given range. If the element is not present, this function returns the iterator to the end of the range.
Code Implementation
C++
// C++ program to check whether the given value
// is present in the vector or not using std::find()
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> vec = {1, 7, 2, 9, 1, 6};
int target = 9;
// Return an iterator pointing to the first
// occurrence of the given target element
auto it = find(vec.begin(), vec.end(), target);
// Check if the target value was found
if (it!=vec.end())
cout << "Found";
else
cout << "Not found";
return 0;
}
Time Complexity: O(n), where n is the number of elements in the vector.
Space Complexity: O(1)
Using Loops
To check if an element is present in a vector, use a loop to iterate through all the elements. If the target element is found at any index, it means the element is present. If no matching value is found after completing the iteration, the target element is not in the vector.
Code Implementation
C++
// C++ program to check whether the given value
// is present in the vector or not using loops
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> vec = {1, 7, 2, 9, 1, 6};
int target = 9;
// Flag to denote whether the element is
// found in the range or not
bool flag = 0;
for (auto i : vec) {
// If the target element found set the
// flag value 1 and break
if (i == target) {
flag = 1;
break;
}
}
// Check if the target value was found
if (flag)
cout << "Found";
else
cout << "Not found";
return 0;
}
Time Complexity: O(n), where n is the number of elements in the vector.
Space Complexity: O(1)
Similar Reads
How to Check If a Set Contains an Element in C++? In C++, the std::set container stores the unique elements in the sorted order. In this article, we will learn how to check if the set contains a given element or not.ExamplesInput: s = {1, 3, 5, 7, 9}, x = 5Output: Element foundExplanation: The element 5 is present in the set.Input: s = {10, 20, 30,
4 min read
How to Access Elements in a Vector in C++? In C++, vector provides fast access to its elements using their position as indexes. In this article, we will discuss different ways to access the element in vector using index in C++.The simplest method to access the elements of vector is by using vector operator [] with the corresponding index val
3 min read
How to Check if a Vector is Empty in C++? A vector is said to be empty when there are no elements present in vector. In this article, we will learn different ways to check whether a vector is empty or not.The most efficient way to check if the vector is empty or not is by using the vector empty() function. Letâs take a look at a simple exam
2 min read
How to Find Index of a Given Element in a Vector in C++? Vectors stores elements in contiguous memory and these elements can be accessed by their indexes. In this article, we will learn the reverse process, i.e., finding the index of the given element in a vector in C++.The simplest way to find the index of the given element in the vector is by using find
3 min read
How to Access the First Element of a Vector in C++? In this article, we will learn how to access the first element of vector in C++.The most efficient way to access the first element of vector is by using vector front() function. Letâs take a look at a simple example:C++#include <bits/stdc++.h> using namespace std; int main() { vector<int
2 min read
How to Find Frequency of an Element in a Vector in C++? In C++, vectors are containers that store the elements in contiguous memory locations just like arrays. The frequency of a specific element means how many times that particular element occurs in a vector. In this article, we will learn how to find the frequency of a specific element in a vector in C
2 min read