Start learning free

LESSON 9 OF 70 · PROJECTS: COMMAND-LINE TOOLS

wc v1 in Kotlin: Count Lines with Array.size

Practice Read the full explanation

Build a tiny version of the Unix wc command. Given a list of lines, return the total number of lines as an integer.

Function signature:

fun countLines(lines: Array<String>): Int

Example:

Input: lines = ["hello", "world", "foo", "bar", "baz"]

Output: 5

Every entry of the list is one line, even an empty string "". An empty list has 0 lines.


The number of lines is just the array's size. Use lines.size.

A one-liner:
class Solution {
    fun countLines(lines: Array<String>): Int {
        return lines.size
    }
}

STEP 1 OF 1
    Ctrl + Enter to check

    KOTLIN · PROJECTS: COMMAND-LINE TOOLS

    Wc V1 in Kotlin, explained

    Problem overview

    The Unix wc command counts things in a text: lines, words and characters. This first version counts only lines. You write fun countLines(lines: Array<String>): Int, which takes the lines of a text and returns how many there are.

    Every entry counts as a line, even an empty string "". For ["hello", "world", "foo", "bar", "baz"] the answer is 5, and for an empty array it is 0.

    Approach

    The input is already split into lines, one array entry per line. Counting the lines is therefore counting the entries, and an array knows that number. You don't need a loop or a counter variable, and you don't need to look at the text of any line.

    Step by step

    1. Return lines.size. size is a property of every Kotlin array that holds the number of entries. It is an Int, which matches the function's return type.

    The solution in Kotlin

    class Solution {
        fun countLines(lines: Array<String>): Int {
            return lines.size
        }
    }
    

    Calling it on a few inputs:

    linesresult
    arrayOf("hello", "world", "foo", "bar", "baz")5
    arrayOf("", "")2
    arrayOf()0
    arrayOf("one\ntwo")1

    The last row is one entry that happens to contain a newline character. The function counts entries, not newline characters, so it still returns 1.

    Kotlin details worth knowing

    • size is a property, not a method. Write lines.size, with no parentheses. Writing lines.size() doesn't compile: the compiler reports that size of type Int cannot be invoked as a function.
    • Array<String> is not List<String>. They are different types, and Kotlin won't convert one to the other when you call a function. Passing a List<String> where an Array<String> is expected is a compile error (argument type mismatch). Both have a size property, so the body of the function would look the same for either.
    • Arrays have a fixed size. You create one with arrayOf(...), and its size never changes afterwards, so size is always the number of entries it was created with.
    • count() gives the same number. lines.count() without arguments also returns the number of entries. size reads the stored value directly, which is why it is the usual choice.
    • The empty case is automatic. An empty array has a size of 0, so no special branch is needed.

    Complexity

    Reading size takes constant time, because the array stores its length, and the function uses no extra space. The number of lines doesn't affect the running time.

    Edge cases and common mistakes

    • Empty strings are lines. arrayOf("", "") has 2 lines. Don't filter out blank entries before counting.
    • Newlines inside an entry. The function counts entries. If one entry contains "\n", it is still one entry here.
    • Parentheses on size. lines.size() is a compile error in Kotlin.
    • Counting by hand. A loop with a counter gives the right answer but adds code and room for off-by-one errors, when the array already holds the number.
    • The empty array. The result is 0, not an error and not -1.

    Try the cases in the table in the editor above. The next version builds on this function.

    Keep going with Kotlin1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free