How to Find All Indexes of an Element in an Array in C++? Last Updated : 18 Mar, 2024 Comments Improve Suggest changes Like Article Like Report 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 indices: 3 4 5Finding All Indexes of an Element in an Array in C++ To find all indexes of an element in an array in C++, we have to iterate through the whole array looking for the target. If the target is found, we will print it. Otherwise, we can continue to the next loop. C++ Program To Find All Indexes of an Element in an ArrayThe below example demonstrates how we can find all indexes of an element in an array in C++. C++ // C++ Program to illustrate how to find all indexes of a // specific element in an array #include <iostream> using namespace std; int main() { // Initializing an array with multiple occurrences of // some elements int myArray[] = { 1, 2, 2, 3, 3, 3, 4, 4, 4, 4 }; int n = sizeof(myArray) / sizeof(myArray[0]); // element to find int target = 3; cout << "The element " << target << " occurred at indices: "; // Iterate through the array for (int index = 0; index < n; ++index) { if (myArray[index] == target) { // If the element matches the search element, // print the index cout << index << " "; } } return 0; } OutputThe element 3 occurred at indices: 3 4 5 Time Complexity: O(n), here n is the size of the array. Auxilliary Space: O(1) Comment More infoAdvertise with us Next Article How to Find All Indexes of an Element in an Array in C++? M maha123 Follow Improve Article Tags : C++ Programs C++ cpp-array CPP Examples Practice Tags : CPP Similar Reads How to Find the Index of an Element in an Array in C++? 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 3 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 All Occurrences of an Element in a List in C++? In C++, std::list is a sequence container that allows non-contiguous memory allocation. As such, it is a doubly linked list that can be traversed in both directions. In this article, we will learn how to find all occurrences of a specific element in a list in C++. Example: Input: myList = {7, 5, 16, 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 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 All Occurrences of an Element in a Deque in C++? In C++, deques are data structures similar to queues provided by the STL library in C++ but unlike queues, deques allow the insertion and deletion of elements from both ends. In this article, we will learn how to find all occurrences of a specific element in a deque in C++. Example Input: deque<i 2 min read How to Find All Occurrences of an Element in a Multiset in C++? In C++, a multiset is a container similar to a set but it allows multiple occurrences of its elements i.e. duplicate values. In this article, we will learn how to find all occurrences of a specific element in a multiset in C++. Example: Input: myMultiset = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4};target = 3Ou 2 min read Like