Home > Data Structures and Algorithms Questions >Find all occurance of an element in an array.
Here's a code snippet to find all occurance of an element in an array.
#include
using namespace std;
int all_occurance(int a[], int n,int key, int index){
if(index == n){
return 0;
}
if(a[index] == key)
cout << index << " ";
all_occurance(a,n, key, index+1);
return 0;
}
int main()
{
int n;
cin>>n;
int arr[n];
for (int i = 0; i < n; ++i)
{
cin>>arr[i];
}
int key;
cin>>key;
int index = 0;
all_occurance(arr, n, key, index);
return 0;
}