fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int findOccurances(vector<int> v, int key, int idx) {
  5. if(idx == v.size()) {
  6. return 0;
  7. }
  8. int ans = 0;
  9. if(v[idx] == key) {
  10. ans++;
  11. }
  12. ans += findOccurances(v, key, idx+1);
  13. return ans;
  14. }
  15.  
  16. int main() {
  17. vector<int> v = {5, 2, 4, 6, 7, 2, 8, 2, 2};
  18. int key = 2;
  19. cout<<findOccurances(v, key, 0);
  20. return 0;
  21. }
Success #stdin #stdout 0s 5336KB
stdin
Standard input is empty
stdout
4