Given an array 'A' of 'N' non-negative integers. Given single integer 'X', you have to simply tell them no. of occurrences of integer 'X' in the given array 'A'.
Example:
Input: n = 5, a = [5,8,1,4,5], x = 2
Output: 2
Approach
C++
#include <bits/stdc++.h>using namespace std;long long numberOfOccurrences(long long n,long long a[], long long x){map<long long, long long> mp;for (long long i = 0; i < n; i++){mp[a[i]]++;}return mp[x];}int main(){long long n = 5;long long a[n] = {5, 8, 1, 4, 5};long long x = 5;cout << numberOfOccurrences(n, a, x) << "\n";return 0;}
No comments:
Post a Comment