Smallest element in an array that is repeated exactly ‘k’ times This is a problem from GeeksForGeeks and I have tried to solve it on my own and I have written this code.


  Given an array of size n, the goal is to find out the smallest number
  that is repeated exactly ‘k’ times where k > 0? Assume that array has
  only positive integers and 1 &lt;= arr[i] &lt; 1000 for each i = 0 to n -1.
  
  Examples:
  
  Input:

arr[] = {2 2 1 3 1}
k = 2

  
  Output: 1
  
  Explanation:
  
  Here in array:
  
  
  2 is repeated 2 times
  1 is repeated 2 times 
  3 is repeated 1 time
  
  
  Hence 2 and 1 both are repeated 'k' times
  
  i.e 2 and min(2, 1) is 1
  
  Input:

arr[] = {3 5 3 2}
k = 1

  
  Output: 2
  
  Explanation:
  
  Both 2 and 5 are repeating 1 time but min(5, 2) is 2


How can I improve this code?

#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;map&gt;

template&lt;class T&gt;
void findElement(std::vector&lt;T&gt;&amp; vec, int k)
{
  std::map&lt;T, int&gt; count;
  for(T x : vec)
  {
    count[x]++;
  }

  typename std::map&lt;T, int&gt;::iterator itr;
  for(itr = count.begin(); itr != count.end(); itr++)
  {
    if(itr-&gt;second == k)
    {
      std::cout &lt;&lt; itr-&gt;first &lt;&lt;'\n';
      return;
    }
  }
  std::cerr &lt;&lt; "No such element \n ";
}

int main()
{
  std::vector&lt;char&gt; v;
  v.push_back('r');
  v.push_back('t');
  v.push_back('q');
  v.push_back('r');
  v.push_back('u');
  v.push_back('q');
  v.push_back('s');
  int k;
  std::cout &lt;&lt; " Enter the number of repetitions you want : ";
  std::cin &gt;&gt; k;
  std::cout &lt;&lt; "The smallest element that has " &lt;&lt; k &lt;&lt;" reptition is : ";
  findElement(v, k);
}
Interface


There's no point in limiting your function inputs to a vector. You can take two input iterators instead. It'll make your function more flexible and more "standard library-like".
One function should do one focused thing. Your function finds such an element  and  prints it as the same time. These concerns are unrelated. You can return std::optional&lt;T&gt; instead to represent either such an element or its absence. Note: it works for C++17 only. You can use boost::optional or return a smart pointer if it's not available. 


Modern C++


There's no need for things like this anymore: 

typename std::map&lt;T, int&gt;::iterator itr;
for(itr = count.begin(); itr != count.end(); itr++)


for (const auto&amp; kv : count) is much better, isn't it?
You can also use initialization list to create a vector with the elements you need: std::vector&lt;char&gt; v({'r', 't',..., 's'})


Performance


for (T x : vec) creates an unnecessary copy. Use for (const T&amp; x : vec) or for (const auto&amp; T : vec) instead.
You can also use std::unordered_map to do the counting and then choose the smallest element (it's more efficient because one can find the minimum in O(n). It's "easier" than sorting the input). One caveat: you need a hash function for T in this case. You can pass it as another template parameter, defaulted to std::hash.