How To Check If Vector Contains A Given Element in C++

Do you have a vector defined in C++ and want to check if a given element exists or not? In this article, I will discuss various ways you can use to check if the vector contains a given element or not.

There are various functions present in C++ that let you know whether the element is present in the vector or not. One such function is std::find(). I will be listing more than 5 methods that you can use to check if the vector contains a given element or not.

1. Using std::find() Method to Check if Vector Contains A Given Element in C++

In C++ standard library we have a function named find(), this function iterates through the given vector and compares the given element, and check if the element is found or not. If the element is found this function returns iteration value else if the element is not found then it returns the end of the list.

Let us see in the below example code the usage of the std::find() method to search a given element.

#include <iostream>
#include <bits/stdc++.h>

using namespace std;
 
int main()
{
    vector<int> givenList = {0,1,2,3,4,5,6,7,8,9};

    //Initializing key to found in the above vector
    int findKey =  4;

    //Using Find Function check if findKey is present or not
    if(find(givenList.begin(), givenList.end(), findKey) != givenList.end()){
        cout<<"The key "<<findKey<<" is found in the vector."<<endl;
    }else{
        cout<<"The Given Key is Not Found in the List"<<endl;
    }

    return 0;
}

Output:

The key 4 is found in the vector.

As you can see in the above code, the given element 4 is present in the vector, and hence using the find() function I was able to search it easily. Note that this method has linear runtime i.e. O(n).

2. Using the count Function To Check If Elements is present in Vector

In C++, there is another function named count that lets you count how many times the given element is present in the vector. This function takes input as a given vector and a given key to search. If the element is present in the list or vector then it will return the number of times it is present else it will return 0.

Let us see in the below example code the usage of Count Function in C++ to search an element.

#include <iostream>
#include <bits/stdc++.h>

using namespace std;
 
int main()
{
    vector<int> givenList = {0,1,2,3,4,5,6,7,8,9};

    //Initializing key to found in the above vector
    int findKey =  9;

    //Using Find Function check if findKey is present or not
    int isFound = count(givenList.begin(), givenList.end(), findKey);

    if(isFound)
        cout<<"Key is Found."<<endl;
    else
        cout<<"Key is Not Found"<<endl;

    return 0;
}

Output:

Key is Found.

As you can see in the above code, the given key 9 is present in the vector, and isFound the variable is used to store the number of times the given element is present in the vector. If the element will not be present in the vector then it will be 0 and then it will be considered as a false value and hence the else condition will be executed.

Note that this method also runs in linear time i.e. O(n) where n is the number of elements present in the given vector.

3. Using binary_search Function to Search an Element in Given Vector

In C++ STL, there is a function named binary_search[1] that lets you search a given element or key in the vector in the most optimized way and its runtime is logarithmic instead of linear.

This method takes a vector as input and the key as input and returns True if the element is found else it returns False if the element is not found. Your vector should be ordered to get the O(log n) runtime complexity.

Let us see the usage of binary_search() function to search elements in vector in the most optimized way.

#include <iostream>
#include <bits/stdc++.h>

using namespace std;
 
int main()
{
    vector<int> givenList = {0,1,2,3,4,5,6,7,8,9};

    //Initializing key to found in the above vector
    int findKey =  1;

    //Using Find_if Function check if findKey is present or not
    auto isFound = binary_search(begin(givenList), end(givenList), findKey);

    if(isFound)
        cout<<"Element is Found in the Given List"<<endl;
    else
        cout<<"Element is not Found"<<endl;
        
    return 0;
}

4. Using find_if Function in C++ to Search for A Given Element in C++

There is another function named find_if[2] that lets you search for a given key or element in the vector and this method returns the iterable value and if the element is not found then it returns the end of the list.

Let us see in the below example code the usage of the find_if function to check an element’s existence.

#include <iostream>
#include <bits/stdc++.h>

using namespace std;
 
int main()
{
    vector<int> givenList = {0,1,2,3,4,5,6,7,8,9};

    //Searching if 1 is present in the list of not.
    //Using lambda Function to Check the elements
    auto isFound = [](int element, int key=6){if(element==key) return 1;else return 0;};

    //Using Find_if Function check if findKey is present or not
    auto check = find_if(begin(givenList), end(givenList), isFound);

    if(check!=end(givenList))
        cout<<"Element is Found in the Given List."<<endl;
    else
        cout<<"Element is not Found."<<endl;

    return 0;
}

Output:

Element is Found in the Given List.

As you can see in the above code, I was able to search the given element by writing a lambda function for comparison of each element in the vector and it returns true if the element is found else it will return false.

How To Check If Vector Contains A Given Element in C++

Wrap Up

I hope you got your answer related to how to check if a vector contains a given element using C++. I have listed down the four best methods that you can use to find the element in the vector. Let me know if you have any better method than the one listed above, I will be happy to add it here.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. How To Declare An Empty Array in C++
  2. How To Convert Char to Int in C++
  3. How To Find The Sum Of An Array Of Numbers in JavaScript
  4. Easiest and Best Way To Convert Int to String in C++
  5. How To Convert a Char to String in C++ Quickly
  6. Parse (split) or Tokenize a String in C++ Using Delimiter
  7. Different Ways to Sort a Vector In C++
  8. std::stoi Convert String To Numbers in C++ [3 Ways]
  9. How To Pop Front in Vector C++ [pop_front]
  10. How To Square Something in C++? 3 Unique Ways
How To Check If Vector Contains A Given Element in C++

Leave a Comment