C++ Program to Find Index of First Occurrence of a Value in Array Last Updated : 11 Mar, 2024 Comments Improve Suggest changes Like Article Like Report In C++, an array is a data structure that stores elements of the same type in contiguous memory locations. In this article, we will learn how to find the index of the first occurrence of a specific value in an array in C++. Example: Input: int arr[] = {5, 7, 1, 2, 3, 7, 1} Target = 1Output: The first occurrence of 1 is at index: 2Finding the Index of the First Occurrence of an Element in an ArrayTo find the index of the first occurrence of a value in an array, we can simply use a loop to iterate through the array and check if the element matches with the target if it matches return the index that is the first occurrence of a value in an array. C++ Program to Find the Index of the First Occurrence of a Value in an Array The below example demonstrates the use of a for loop to find the index of the first occurrence of a value in a given array in C++. C++ // C++ Program to show how to Find the Index of the First Occurrence of // a Value in an Array #include <iostream> using namespace std; int main() { // initializing array int arr[] = { 5, 7, 1, 2, 3, 7, 1 }; int n = sizeof(arr) / sizeof(arr[0]); // target whose first occurence need to be searched int target = 1; int index = -1; // using a for loop to find the first occurrence of // the element in the array. for (int i = 0; i < n; i++) { if (arr[i] == target) { index = i; break; } } // if target is found print it's index if (index != -1) { cout << "The first occurrence of " << target << " is at index: " << index << endl; } // else element not found else { cout << "Element not found." << endl; } return 0; } OutputThe first occurrence of 1 is at index: 2 Time Complexity: O(N), here N is the number of elements in the array.Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article C++ Program to Find Index of First Occurrence of a Value in Array D denzirop9v Follow Improve Article Tags : C++ Programs C++ cpp-array C++ Array Programs CPP Examples +1 More Practice Tags : CPP Similar Reads How to Count the Number of Occurrences of a Value in an Array in C++? In C++, an array is a data structure that stores the collection of the same type of data in a contiguous memory location. In this article, we will learn how to count the number of occurrences of a value in an array in C++. Example: Input: arr= {2, 4, 5 ,2 ,4 , 5, 2 , 3 ,8}Target = 2Output: Number of 2 min read How to Find First Occurrence of an Element in a Set in C++? In C++, a set is an ordered container that stores its unique values in some given order. In this article, we will see how to find the first occurrence of a specific element in a set in C++ STL. For Example, Input: mySet = {1, 2, 3, 8, 9, 11} Target = 9 Output: Element found at Index: 4Find the First 2 min read How to Find the First Occurrence of an Element in a Vector? In C++, the vector is a dynamic array that is defined in the STL template library inside the <vector> header. In this article, we will learn how to find the first occurrence of a specific element in a vector in C++. For Example Input: vector<int>v = {5, 7, 1, 2, 3, 7, 1} Target = 1 Outpu 2 min read How to Find First Occurrence of an Element in a List in C++? In C++, the list is a sequence container that stores data in non-contiguous memory allocation. It is defined in the STL (Standard Template Library) inside the <list> header. In this article, we will learn how to find the first occurrence of a specific element in a list in C++. Example: Input: 2 min read How to Find First Occurrence of an Element in a Deque in C++? In C++, deques also known as double-ended queues are sequence containers with the feature of insertion and deletion on both ends. In this article, we will learn how to find the first occurrence of a specific element in a deque in C++. Example Input: myDeque ={2, 1, 5, 3, 4, 2, 5} Target=5 Output: Th 2 min read How to Find First Key-Value Pair in a Map in C++? In C++ STL, a map is a container that stores key-value pairs in an ordered or sorted manner. In this article, we will learn how to find the first key-value pair in a Map. Example: Input: myMap = {{1, "C++"}, {2, "Java"}, {3, "Python"}, {4, "JavaScript"}}; Output: First key-Value Pair : (1, C++)Getti 2 min read C++ Program to Find the Frequency of Elements in an Array In C++, arrays are a type of data structure that can store a fixed-size sequential collection of elements of the same type. In this article, we will learn how to find the frequency of elements in an array in C++. Example: Input: Array: {1, 2, 3, 4, 2, 1, 3, 2, 4, 5} Output: Element: 1, Frequency: 2 2 min read How to Find the Second Occurrence of an Element in Vector in C++? In C++, vector containers store the data in a contiguous memory location like arrays and also can resize themselves to store more elements. In this article, we will learn how to find the second occurrence of a specific element in a vector in C++. Example Input: myVector = {20, 30, 10, 50, 10, 80, 10 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 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 Like