How to Find the Index of an Element in an Array in C++? Last Updated : 21 Oct, 2024 Comments Improve Suggest changes Like Article Like Report Given an array of n elements, the task is to find the index of a specific element in C++.ExamplesInput: arr[] = {11, 13, 9, 21, 51, 1000}, val = 9Output: 2Explanation: As the value 9 is present at index 2.Input: arr[] = {5, 8, 12, 9, 11, 32}, val = 11Output: 4Explanation: As the value 11 is present at index 4.Following are the 2 different ways to find the index of a specific element in the given array in C++:Table of ContentUsing std::find()Manually Using Linear SearchUsing std::find()To find the index of an element in an array in C++, we can use the std::find() function that searches the element in the given range and returns the pointer to the matching element. We can then find the index by subtracting the pointer to the beginning of the array from this pointer.Syntax of std::find()find(arr, arr + n, val);where n is the size of array arr.Example C++ // C++ Program to find the index of an // element in an array using std::find() #include <bits/stdc++.h> using namespace std; int main() { int arr[] = {11, 13, 9, 21, 51, 1000}; int n = sizeof(arr) / sizeof(arr[0]); int val = 9; // Using find() to get the pointer to the // first occurence of value auto ptr = find(arr, arr + n, val); // Getting index from pointer int idx = ptr - arr; // Checking if the element found or not if (idx >= n) cout << "Element not Found!"; else cout << idx; return 0; } Output2Time Complexity: O(n), where n is the size of the array. Auxiliary Space: O(1)Manually Using Linear SearchWe can also find the index of an element in array manually using linear search algorithm which is nothing but running the loop in the given range and checking if any element is equal to the searched element using if statement.Example C++ // C++ Program for finding the index of an // element in an array using linear search #include <bits/stdc++.h> using namespace std; int main() { int arr[] = {11, 13, 9, 21, 51, 1000}; int n = sizeof(arr) / sizeof(arr[0]); int val = 9; // Index variable int idx; // Searching for value in the array using loop for (idx = 0; idx < n; idx++) { if (arr[idx] == val) { break; } } // Checking if the element found or not if (idx == -1) cout << "Element not Found!\n"; else cout << idx; return 0; } Output2Time Complexity: O(n), where n is the size of the array. Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Find the Index of an Element in an Array in C++? A aayushi2402 Follow Improve Article Tags : C++ Programs C++ STL cpp-array CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Find All Indexes of an Element in an Array in C++? In C++, an array is a collection of elements of the same type placed in contiguous memory locations. In this article, we will learn how to find all indexes of a specific element in an array in C++. Example: Input: myArray = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4}; target = 3Output: The element 3 occurred at 2 min read How to Find the Sum of Elements in an Array in C++? In C++, an array is the collection of similar data elements that are stored in the contiguous memory location and we can access these elements directly by their index value. In this article, we will learn how to find the sum of elements in an array in C++. Example:Input: myVector = {1, 2, 3, 4, 5} O 2 min read Find index of an element in a Set in C++ Given a set S consisting of N integers and an element K, the task is to find the index of the element K in the set S. If the element is not present in S, print -1. Examples: Input: N = 5, S = {1, 2, 3, 4, 6} K = 6Output: 5Explanation: 6 is the 5th element in S. Input: N = 5, S = {1, 2, 3, 4, 6}, K = 2 min read How to Find the Second Smallest Element in an Array in C++? In C++, arrays are data structures that store the collection of data elements of the same type in contiguous memory locations. In this article, we will learn how to find the second smallest element in an array in C++. Example:Input:myArray = {10, 5, 8, 2, 7, 3, 15};Output:The second smallest element 3 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 Find the Unique Elements in an Array in C++? In C++, an array is a data structure that is used to store multiple values of similar data types in a contiguous memory location. In this article, we will learn how to find the unique elements in an array in C++. Example: Input: array = {1, 2, 1, 2, 2, 3, 4} Output: Unique elements in the array: 1 2 2 min read How to Find All Occurrences of an Element in a Set in C++? Finding the all occurrences of a specific element in a set using the C++ STL is a very efficient process that is done with the help of std::set::distance() member function. In this article, we'll explore how to find the first element in a set using the C++ STL. For Example,Input:mySet = {1, 2, 4, 3, 2 min read How to Find the Mode in a 2D Array in C++? A mode is a number that occurs most frequently in comparison to other numbers in a given dataset. In this article, we will find the mode in a 2D array of integers in C++. Input:myArray = { {1, 2, 2, 3}, {3, 4, 5, 5}, {5, 6, 7, 8} }Output: 5Mode in 2D ArrayTo find a mode of numbers in a 2D array, we 2 min read How to Find the Last Occurrence of an Element in a Set in C++? In C++, a set is a container that stores unique elements in a sorted order and elements are accessed and traversed using iterators. In this article, we will learn how to find the last occurrence of a specific element in a set in C++. Example Input:set<int> s = {1, 2, 3, 4, 5, 6, 7, 8, 9}; Key 2 min read How to Find the Mode of Numbers in an Array in C++? Mode of any dataset is the item that occurs most frequently in it. In this article, we will find the mode of numbers in an unsorted array in C++. For Example,Input: myArray = { 1, 2, 3, 4, 5, 2, 3, 2, 2, 4, 2 } Output: Mode : 2Finding Mode of Array Elements in C++To find the mode of the array elemen 2 min read Like