
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check If Item is Included in Array in C++
An array is a linear sequential data structure to hold homogeneous data in consecutive memory locations. Like other data structures, an array also must?have features to insert, delete, traverse and update elements in some efficient way. In C++ our arrays are static. There are a few dynamic array structures also available in C++. For a static array, there may be a Z number of elements that can be stored inside that array. And till now we have n elements into it. In this article, we will see how to check whether an element is present inside an array or not using C++.
Understanding the concept with examples
Given array A = [10, 14, 65, 85, 96, 12, 35, 74, 69] Check whether 96 is present inside A or not. Yes, it is present, so return True.
In the given example, we have an array A with nine elements in it. We are going to check whether element 96 is there inside A or not. Since it is present then we return true, otherwise for some element that is not present return false. To check the element is inside an array, we can perform searches. For the non?sorted given array, we need to perform a linear search. Let us see the algorithm for a clear understanding.
Algorithm
Take an array A, and element k to check whether e is inside A or not
-
For each element e in A, do
-
if e is the same as k, then
return true
end if
-
end for
return false
Example
#include <iostream> # define Z 50 using namespace std; void displayArr(int arr[], int n){ for( int i = 0; i < n; i++ ){ cout << arr[ i ] << ", "; } cout << endl; } string isInsideArray( int A[], int n, int k ){ for( int i = 0; i < n; i++) { if( A [ i ] == k ) { return "Yes"; } } return "No"; } int main() { int arr[ Z ] = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14}; int n = 12; cout << "Array elements: "; displayArr( arr, n ); cout << "Is 86 inside the array? :"; cout << isInsideArray( arr, n, 86 ); cout << "\nIs 900 inside the array? :"; cout << isInsideArray( arr, n, 900 ); cout << "\nIs 65 inside the array? :"; cout << isInsideArray( arr, n, 65 ); }
Output
Array elements: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, Is 86 inside the array? :Yes Is 900 inside the array? :No Is 65 inside the array? :Yes
Using Vectors in C++ STL
In the above example, we are using static arrays where we need to perform the linear or sequential search to check whether the element is inside the array or not. In this section, we will use the vectors from C++ STL. The vectors are dynamic data structures with plenty of different functions present in them. To search for an element inside the vector, we can use the find() method. This method comes inside the ?algorithm' header file. Let us see the code for it.
Example
#include <iostream> #include <vector> #include <algorithm> # define Z 50 using namespace std; void displayArr( vector<int> v ){ for( int i = 0; i < v.size() ; i++ ){ cout << v[ i ] << ", "; } cout << endl; } string isInsideArray( vector<int> A, int k ){ if( find( A.begin(), A.end(), k ) != A.end() ) { return "Yes"; } return "No"; } int main() { vector<int> A = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14}; cout << "Array elements: "; displayArr( A ); cout << "Is 86 inside the array? :"; cout << isInsideArray( A, 86 ); cout << "\nIs 900 inside the array? :"; cout << isInsideArray( A, 900 ); cout << "\nIs 65 inside the array? :"; cout << isInsideArray( A, 65 ); }
Output
Array elements: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, Is 86 inside the array? :Yes Is 900 inside the array? :No Is 65 inside the array? :Yes
Besides find(), there is another method that we can use to check whether an element is inside a vector or not, which is the default count() function for vector objects. When the count is 0, it indicates the element is not present, otherwise it is present. The implementation is like below:
Example
#include <iostream> #include <vector> #include <algorithm> # define Z 50 using namespace std; void displayArr( vector<int> v ){ for( int i = 0; i < v.size() ; i++ ){ cout << v[ i ] << ", "; } cout << endl; } string isInsideArray( vector<int> A, int k ){ if( count( A.begin(), A.end(), k ) == 0 ) { return "No"; } return "Yes"; } int main() { vector<int> A = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14}; cout << "Array elements: "; displayArr( A ); cout << "Is 86 inside the array? :"; cout << isInsideArray( A, 86 ); cout << "\nIs 900 inside the array? :"; cout << isInsideArray( A, 900 ); cout << "\nIs 65 inside the array? :"; cout << isInsideArray( A, 65 ); }
Output
Array elements: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, Is 86 inside the array? :Yes Is 900 inside the array? :No Is 65 inside the array? :Yes
Conclusion
In this article, we have seen how to check whether an element is present inside an array or not. We are taking static arrays and also vectors. For static arrays, we are searching through the array using the linear searching method. Since we are considering the elements in the array are not sorted, linear searches are used. Otherwise, we can use binary searches for sorted data. In the second and third methods, we are using vectors and the find() and count methods are used to check whether the element is an indie vector or not. There may be some other possible methods using STL using sets or set_intersection() to do the same.