Header Ad

HackerRank Day 21 Generics 30 days of code solution

In this HackerRank Day 21 Generics 30 days of code 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.


Day 21 Generics 30 days of code solution HackerRank


Problem solution in java programming.

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++ programming.

/**
*    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;

}

Post a Comment

0 Comments