Sort by

recency

|

361 Discussions

|

  • + 0 comments

    import java.util.*;

    public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in);

        while (sc.hasNextInt()) {
            int t = sc.nextInt();
    
            // Peek the next token to check if it is an int
            if (sc.hasNextInt()) {
                Integer[] arr = new Integer[t];
                for (int i = 0; i < t; i++) {
                    if (sc.hasNextInt()) {
                        arr[i] = sc.nextInt();
                    }
                }
                printArray(arr);
            } else {
                String[] arr = new String[t];
                for (int i = 0; i < t; i++) {
                    if (sc.hasNext()) {
                        arr[i] = sc.next();
                    }
                }
                printArray(arr);
            }
        }
    
        sc.close();
    }
    
    public static <T> void printArray(T[] array) {
        for (T item : array) {
            System.out.println(item);
        }
    }
    

    }

  • + 0 comments

    TypeScript

    function printArray<T>(array: T[]): void {
        for (let i = 0; i < array.length; i++) {
            console.log(array[i]);
        }
    }
    
    function main() {
    
        let n: number = parseInt(readLine().trim(), 10);
    
        const intArray: number[] = new Array(n);
        for (let i = 0; i < n; i++) {
            intArray[i] = parseInt(readLine().trim(), 10);
        }
    
        n = parseInt(readLine().trim(), 10);
    
        const stringArray: string[] = new Array(n);
        for (let i = 0; i < n; i++) {
            stringArray[i] = readLine().trim();
        }
    
        printArray<number>(intArray);
        printArray<string>(stringArray);
    }
    
  • + 0 comments

    //javaScript

    function printArray(array) { for (const element of array) { console.log(element); } }

  • + 0 comments
    c# csharp
    
    static void PrintArray<T>(T[] value)
        {
      //Linq ArrayForEach
            Array.ForEach(value, rec => Console.WriteLine($"{rec}"));
      //or Normal ForEach, both is working in similar way
      // foreach(var rec in value)
      //Console.WriteLine($"{rec}"); 
          }
    
  • + 0 comments
    template <typename T>
    void printArray(const vector<T>& vec) {
        for (const T& element : vec) {
            cout << element << endl;
        }
    }