Home

Easy one

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 longlong longmp;
    for (long long i = 0i < ni++)
    {
        mp[a[i]]++;
    }

    return mp[x];
}
int main()
{

    long long n = 5;
    long long a[n] = {58145};
    long long x = 5;

    cout << numberOfOccurrences(nax<< "\n";

    return 0;
}


No comments:

Post a Comment