### C++: Program to find smallest element is an array which is repeated exactly 'k' times

This is a problem from GeeksForGeeks . I have tried to write different code for the problem.

The problem says we have an array of elements . We have to find the smallest element which is repeated exactly

Here we want to find the smallest element in the vector which is repeated exactly 2 times.

Here r is repeated 2 times, t is repeated 1 time, q is repeated 2 times, u is repeated 1 time and s is repeated 1 time.

The program for the above problem is

#include <iostream>

#include <vector>

#include <map>

template<class T>

void findElement(std::vector<T>& vec, int k)

{

std::map<T, int> count;

for(const T& x : vec)

{

count[x]++;

}

typename std::map<T, int>::iterator itr;

for(itr = count.begin(); itr != count.end(); itr++)

{

if(itr->second == k)

{

std::cout << itr->first <<'\n';

return;

}

}

std::cerr << "No such element \n ";

}

int main()

{

std::vector<char> 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 << " Enter the number of repetitions you want : ";

std::cin >> k;

std::cout << "The smallest element that has " << k <<" reptition is : ";

findElement(v, k);

}

Here we have use std::map which will count the occurrence of the elements.

for(const T& x : vec)

{

count[x]++;

}

Here we have passed the reference of the elements stored in vector and then count maps these elements with their number of occurrence.

To understand better you should read C++: STL Iterators and C++: Understanding Template

You can download this code from here

You may also like

The problem says we have an array of elements . We have to find the smallest element which is repeated exactly

*k*times.Here we want to find the smallest element in the vector which is repeated exactly 2 times.

Here r is repeated 2 times, t is repeated 1 time, q is repeated 2 times, u is repeated 1 time and s is repeated 1 time.

The program for the above problem is

#include <iostream>

#include <vector>

#include <map>

template<class T>

void findElement(std::vector<T>& vec, int k)

{

std::map<T, int> count;

for(const T& x : vec)

{

count[x]++;

}

typename std::map<T, int>::iterator itr;

for(itr = count.begin(); itr != count.end(); itr++)

{

if(itr->second == k)

{

std::cout << itr->first <<'\n';

return;

}

}

std::cerr << "No such element \n ";

}

int main()

{

std::vector<char> 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 << " Enter the number of repetitions you want : ";

std::cin >> k;

std::cout << "The smallest element that has " << k <<" reptition is : ";

findElement(v, k);

}

Here we have use std::map which will count the occurrence of the elements.

for(const T& x : vec)

{

count[x]++;

}

Here we have passed the reference of the elements stored in vector and then count maps these elements with their number of occurrence.

To understand better you should read C++: STL Iterators and C++: Understanding Template

You can download this code from here

You may also like

**C++: Sorting Elements according to Frequency****What is "namespace" and why do we use it?****C++: Program to Add and Multiply the digits of a Number****C++: Check if the String is Palindrome****C++ Program: Counting letters,words,etc in input**
yes its working on boreland

ReplyDelete