fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. // Optimised approach: using frequency map
  5. int main()
  6. {
  7. int arr[11] = { 8 , 3 , 5 , 2 , 3 , 1 , 6 , 5 , 7 , 4 , 3};
  8. int n=11;
  9.  
  10. /*
  11.   Element frequency
  12.   8 1
  13.   3 3
  14.   5 2
  15.   2 1
  16.   1 1
  17.   6 1
  18.   7 1
  19.   4 1
  20.  
  21.   */
  22. unordered_map<int,int>frequencymap;
  23. for(int i=0; i<n; i++)
  24. {
  25. frequencymap[arr[i]] = frequencymap[arr[i]] + 1;
  26. }
  27.  
  28.  
  29. int q;
  30. cout<<"Enter No of Queries"<<endl;
  31. cin>>q; // 2
  32.  
  33. for(int i=0; i<q; i++)
  34. {
  35. int x;
  36. cout<<"Enter No to find frequency"<<endl;
  37. cin>>x; // 5 6
  38.  
  39. cout<<"Frequency of "<<x<< " = " <<frequencymap[x]<<endl; // 5=2 6=1
  40. }
  41.  
  42. return 0;
  43. }
Success #stdin #stdout 0.01s 5280KB
stdin
2
5 6
stdout
Enter  No of Queries
Enter  No to find frequency
Frequency of 5 = 2
Enter  No to find frequency
Frequency of 6 = 1