Day 21 Generics Hackerrank Solution in C++ | 30 Days of Code

Day 21 Generics Hackerrank Solution in C++ and C. We have to just print the Generics array, we can do this by using the AUTO keyword in our program. I didn't understand why people don't use AUTO Keyword in C++. as you can see that Generics array means all types of data types in an array so for that this is a best practice to use the AUTO keyword in the problem.

Day 21 Generics Hackerrank Solution in C++

Objective: Today we're discussing Generics; be aware that not all languages support this construct, so fewer languages are enabled for this challenge. Check out the Tutorial tab for learning materials and an instructional video!

Task: Write a single generic function named printArray; this function must take an array of generic elements as a parameter (the exception to this is C++, which takes a vector). The locked Solution class in your editor tests your function.

Note: You must use generics to solve this challenge. Do not write overloaded functions.

Input Format: The locked Solution class in your editor will pass different types of arrays to your printArray function.

Constraints: You must have exactly 1 function named printArray.

Output Format: Your printArray function should print each element of its generic array parameter on a new line.

Copy the code and paste it into the Hackerrank editor and click to submit button, but before this process, you have to click the below link, this link takes to you the same problem Now you have to just log in if you are already not logged in and done.

Submit this solution: Click Here

Generics Hackerrank Solution in C++

#include <iostream>

#include <vector>

#include <string>

using namespace std;

template < class T >
  void printArray(vector < T > i) {
    for (int j = 0; j < i.size(); j++)
      cout << i[j] << endl;

void printArray(vector < auto > a) {
  for (auto i: a)
    cout << i << endl;

int main() {
  int n;

  cin >> n;
  vector < int > int_vector(n);
  for (int i = 0; i < n; i++) {
    int value;
    cin >> value;
    int_vector[i] = value;

  cin >> n;
  vector < string > string_vector(n);
  for (int i = 0; i < n; i++) {
    string value;
    cin >> value;
    string_vector[i] = value;

  printArray < int > (int_vector);
  printArray < string > (string_vector);

  return 0;

Day 21 Generics Hackerrank Solution Output

Day 21 Generics Hackerrank Solution Output

Similar to 30 Days of Code

Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me