fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4.  
  5. template<class T>
  6. void findElement(std::vector<T>& vec, int k)
  7. {
  8. std::map<T, int> count;
  9. for(T x : vec)
  10. {
  11. count[x]++;
  12. }
  13.  
  14. typename std::map<T, int>::iterator itr;
  15. for(itr = count.begin(); itr != count.end(); itr++)
  16. {
  17. if(itr->second == k)
  18. {
  19. std::cout << itr->first <<'\n';
  20. return;
  21. }
  22. }
  23. std::cerr << "No such element \n ";
  24. }
  25.  
  26. int main()
  27. {
  28. std::vector<char> v;
  29. v.push_back('r');
  30. v.push_back('t');
  31. v.push_back('q');
  32. v.push_back('r');
  33. v.push_back('u');
  34. v.push_back('q');
  35. v.push_back('s');
  36. int k;
  37. std::cout << " Enter the number of repetitions you want : ";
  38. std::cin >> k;
  39. std::cout << "The smallest element that has " << k <<" reptition is : ";
  40. findElement(v, k);
  41. }
  42.  
Success #stdin #stdout #stderr 0s 16064KB
stdin
Standard input is empty
stdout
 Enter the number of repetitions you want : The smallest element that has 11123 reptition is : 
stderr
No such element