Check if an element exists in a C++ array
This post will discuss how to check if an element exists in an array in C++.
C++ standard library offers several algorithms that can efficiently search an array for the specified element. These are discussed below in detail:
1. Using std::find
A simple and elegant solution is to use the std::find
function to find a value in an array. It returns an iterator to the first occurrence of the matching element, or an iterator to the end of the range if that element is not found.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <algorithm> int main() { int arr[] = {5, 3, 7, 6, 8, 2}; int target = 7; int n = sizeof(arr) / sizeof(*arr); bool exists = std::find(arr, arr + n, target) != arr + n; if (exists) { std::cout << "Element found"; } else { std::cout << "Element not found"; } return 0; } |
Output:
Element found
This is recommended approach since the algorithm stops searching as soon as a match is found. With C++11, we can pass an iterator to the beginning and end of an array to it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <algorithm> #include <array> int main() { int arr[] = {5, 3, 7, 6, 8, 2}; int target = 7; bool exists = std::find(std::begin(arr), std::end(arr), target) != std::end(arr); if (exists) { std::cout << "Element found"; } else { std::cout << "Element not found"; } return 0; } |
Output:
Element found
We can also implement a generic contains()
routine, which offers type-safety and works for both C-style arrays and STL containers:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream> #include <algorithm> template<class C, typename T> bool contains(C&& c, T t) { return std::find(std::begin(c), std::end(c), t) != std::end(c); }; int main() { int arr[] = {5, 3, 7, 6, 8, 2}; int target = 7; bool exists = contains(arr, target); if (exists) { std::cout << "Element found"; } else { std::cout << "Element not found"; } return 0; } |
Output:
Element found
2. Using any_of()
function
With C++11, we can use the any_of()
function that returns true
if and only if the specified predicate holds for any of the elements in the specified range.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream> #include <algorithm> #include <array> int main() { int arr[] = {5, 3, 7, 6, 8, 2}; int target = 7; bool exists = std::any_of(std::begin(arr), std::end(arr), [&](int i) { return i == target; }); if (exists) { std::cout << "Element found"; } else { std::cout << "Element not found"; } return 0; } |
Output:
Element found
3. Using std::count
Another option is to use the standard algorithm std::count
, which returns the count of elements matching a value within the specified range. This, however, performs slower than the std::find
function, since it might end up traversing the complete array to get the element’s count.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <algorithm> int main() { int arr[] = {5, 3, 7, 6, 8, 2}; int target = 7; bool exists = std::count(std::begin(arr), std::end(arr), target) > 0; if (exists) { std::cout << "Element found"; } else { std::cout << "Element not found"; } return 0; } |
Output:
Element found
Alternatively, we can use the std::count_if
function that returns the total number of elements satisfying a predicate.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> #include <algorithm> int main() { int arr[] = {5, 3, 7, 6, 8, 2}; int target = 7; bool exists = std::count_if(std::begin(arr), std::end(arr), [&](int i) { return i == target; }) > 0; if (exists) { std::cout << "Element found"; } else { std::cout << "Element not found"; } return 0; } |
Output:
Element found
4. Using std::binary_search
If the array is sorted, the fastest option is to use the std::binary_search
algorithm. It returns true
if the element is found in the specified range, and false
otherwise. For an unsorted array, we can sort the array and then call the binary search routine. This, however, increases the time complexity to O(nlog(n)).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include <iostream> #include <algorithm> bool isExists(int ints[], int n, int target) { if (n <= 0) { return false; } std::sort(ints, ints + n); return std::binary_search(ints, ints + n, target); } int main() { int arr[] = {1, 3, 5, 7, 9}; int target = 7; int n = sizeof(arr) / sizeof(*arr); bool exists = isExists(arr, n, target); if (exists) { std::cout << "Element found"; } else { std::cout << "Element not found"; } return 0; } |
Output:
Element found
5. Using Boost
Another good alternative is to use C++ boost library. It has boost::algorithm::any_of_equal function in header <boost/algorithm/cxx11/any_of.hpp>
, which returns true
if any of the elements in the specified array is equal to the specified value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <boost/algorithm/cxx11/any_of.hpp> int main() { int arr[] = {5, 3, 7, 6, 8, 2}; int target = 7; bool exists = boost::algorithm::any_of_equal(arr, target); if (exists) { std::cout << "Element found"; } else { std::cout << "Element not found"; } return 0; } |
Output:
Element found
6. Using Custom Routine
Finally, we can loop through the array to linearly search for the specified element. This can be implemented as follows in C++.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <iostream> #include <algorithm> int main() { int arr[] = {5, 3, 7, 6, 8, 2}; int target = 7; bool exists = false; for (int i: arr){ if (i == target) { exists = true; break; } } if (exists) { std::cout << "Element found"; } else { std::cout << "Element not found"; } return 0; } |
Output:
Element found
That’s all about checking if an element exists in an array in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)