Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerRank Day 21 Generics 30 days of code solution

YASH PAL, 31 July 202413 October 2025

HackerRank Day 21 Generics solution – In this problem set, we need to write a function printArray that can take an array of generic elements as a parameter and that function can print each element of its generic array parameter on a new line.

Objective
In this challenge we will discuss about the Generics.

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  function named printArray.

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

Generics Problem solution in java.

class Printer<E> {
   //Write your code here
    public <E> void printArray(E[] array) {
        for (E element : array) {
            System.out.println(element);
        }
    }
}

Problem solution in c++.

/**
*    Name: printArray
*    Print each element of the generic vector on a new line. Do not return anything.
*    @param A generic vector
**/

template <class T>
void printArray(vector<T> vec){

   for(int i=0; i<vec.size(); i++)
   cout<<vec[i]<<endl;

}
30 days of code coding problems solutions HackerRank

Post navigation

Previous post
Next post

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes