Start learning free

LESSON 125 OF 348 · LOOPS & CONDITIONALS

Print Positive Numbers From an Array in Kotlin

Practice Read the full explanation

Given an array of integers, print to the console the positive numbers, one on each line.

A number n is positive if it is strictly greater than 0.

Example:

Input: nums = [3, -2, 0, 4, -4]
Output (console):
3
4

Note:

You have to print the numbers to the console, your function shouldn't return anything.


STEP 1 OF 3
    Ctrl + Enter to check

    KOTLIN · LOOPS & CONDITIONALS

    Print Positive Numbers From Array in Kotlin, explained

    Problem overview

    The function printPositiveNumbers(nums: IntArray) receives an array of integers and must print, one per line, those that are positive. A number is positive when it is strictly greater than 0, so zero does not count. The function prints and returns nothing. The numbers come out in the order they appear in the array.

    For nums = [3, -2, 0, 4, -4] the output is 3 followed by 4. The values -2 and -4 are negative and 0 is neither positive nor negative, so all three are skipped.

    Approach: look at each number once

    The task has two parts: go through every element, and decide for each one whether to print it. A loop handles the first part and an if handles the second. Nothing needs to be remembered between elements, so no extra variables are needed.

    This is a filter in its simplest form. Each element is checked on its own, which means the order of the array does not matter for correctness, and the output keeps the original order.

    Step by step

    1. Loop over nums with for (num in nums). Each pass gives num the next element. The loop variable needs no val or type, since Kotlin knows it is an Int from the array.
    2. Test the number with if (num > 0). Use > and not >=. With >= the zero in the example would be printed too.
    3. Print inside the if. println(num) prints the value and a line break, so each result sits on its own line.

    The solution in Kotlin

    class Solution {
        fun printPositiveNumbers(nums: IntArray) {
            for (num in nums) {
                if (num > 0) {
                    println(num)
                }
            }
        }
    }
    

    Kotlin details worth knowing:

    • IntArray. It is Kotlin's array of primitive ints. It is written intArrayOf(3, -2, 0, 4, -4), and it is a different type from Array<Int> and from List<Int>. A function that takes an IntArray cannot be called with a list.
    • Looping by value. for (num in nums) hands you the elements themselves. You only need indices if you want the position as well, in which case you can write for (i in nums.indices) and use nums[i].
    • The loop variable is read-only. Assigning to num inside the loop does not compile, and it would not change the array anyway.
    • No return value. The function's return type is Unit, which Kotlin uses when you write no return type. It prints and finishes.

    Complexity

    Time: O(n) for an array of n numbers. Each element is read once and compared once.

    Space: O(1). Nothing is stored. The output goes straight to the console.

    Edge cases and common mistakes

    • Zero. It is not positive. num >= 0 is the classic slip and prints it.
    • An empty array. The loop body never runs, so nothing is printed. No special case is needed.
    • No positive numbers. An array like [-1, 0, -7] prints nothing.
    • All positive numbers. The function prints the whole array, one number per line.
    • Returning instead of printing. The task asks for console output. Collecting the numbers in a list and returning it would not satisfy it.
    • Using print instead of println. The numbers would run together on one line, as in 34 instead of two lines.
    • Duplicates. A repeated positive value is printed each time it appears. [2, 2] prints 2 twice.

    Testing

    • [3, -2, 0, 4, -4] prints 3 and 4.
    • [] prints nothing.
    • [-5, -1] prints nothing.
    • [0] prints nothing.
    • [1, 2, 3] prints 1, 2 and 3 on separate lines.
    Keep going with KotlinAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free