Start learning free

LESSON 154 OF 414 · LOOPING OVER ARRAY INDICES

Get Full Names in Scala: Pairing Two Arrays by Index

Practice Read the full explanation

Given two arrays of strings named firstNames and lastNames, return a list containing the full names (separated by one space)

Example:

Input:   firstNames = ["John","Andy","Mary"]
         lastNames = ["Doe","Smith","Johnson"]

Output:  ["John Doe", "Andy Smith", "Mary Johnson"]
STEP 1 OF 5
    Ctrl + Enter to check

    SCALA · LOOPING OVER ARRAY INDICES

    Get Full Names in Scala, explained

    Problem overview

    The method getFullNames receives two arrays of strings, firstNames and lastNames. The name at position i in one array belongs with the name at position i in the other. The method returns a List[String] of full names, each made of a first name, one space and a last name.

    For firstNames = ["John", "Andy", "Mary"] and lastNames = ["Doe", "Smith", "Johnson"], the result is ["John Doe", "Andy Smith", "Mary Johnson"]. John is at index 0 in the first array and Doe is at index 0 in the second, so they form the first full name, and so on.

    Approach

    The two arrays are parallel: they line up by index. So the work is one pass over the indices. At each index you read one string from each array, join them with a space and collect the result. You need a place to collect results, and a growable buffer is the natural choice because the number of names is only known at run time, while an array has a fixed size.

    Scala's immutable List is the return type, so the buffer is converted to a list at the end.

    Step by step

    1. Create the buffer. Declare names as an empty scala.collection.mutable.ArrayBuffer[String]. Writing the full package path means no import is needed.
    2. Loop over the indices. Use for (i <- firstNames.indices). The index is needed because the same position is read from two arrays.
    3. Build one full name. Inside the loop, join firstNames(i), a single space " " and lastNames(i) with +. The space is a separate string in the middle, so it is easy to forget.
    4. Append it. names += name adds one element at the end of the buffer.
    5. Convert and return. After the loop, names.toList copies the buffer into an immutable List, which is what the declared return type asks for.

    The solution in Scala

    class Solution {
        def getFullNames(firstNames: Array[String], lastNames: Array[String]): List[String] = {
            val names = scala.collection.mutable.ArrayBuffer[String]()
            for (i <- firstNames.indices) {
                val name = firstNames(i) + " " + lastNames(i)
                names += name
            }
            return names.toList
        }
    }
    

    Some Scala details behind these lines:

    • val with a mutable buffer. val names means the name names always refers to the same buffer. The buffer's contents can still grow, which is why names += name is allowed. Reassigning names itself would not compile.
    • Arrays, buffers and lists. An Array is mutable but its size is fixed. An ArrayBuffer grows as you append. A List is immutable. Collecting into a buffer and converting once at the end is a common pattern.
    • Parentheses on a buffer and on an array. ArrayBuffer[String]() calls the factory with no arguments, and element access also uses parentheses, firstNames(i).

    A shorter alternative

    Scala can also pair two collections directly with zip. firstNames.zip(lastNames) produces pairs, and mapping each pair to a joined string gives the same list. The loop version above is what the lesson uses, and it makes the index-based thinking explicit.

    Complexity

    Time: O(n * m) in the worst case, where n is the number of names and m the length of a name. The loop runs n times, and each pass builds one string by copying its characters. If you treat name length as a small constant, this is simply O(n). Appending to an ArrayBuffer takes constant time on average, and toList is one more pass over the n elements.

    Space: O(n). The result holds n strings, and the buffer holds the same n before it is converted.

    Edge cases and common mistakes

    • Empty arrays. With two empty arrays, firstNames.indices is empty, the loop doesn't run, and the method returns an empty list.
    • Forgetting the space. firstNames(i) + lastNames(i) gives "JohnDoe". The " " between them makes "John Doe".
    • Returning the buffer. The declared type is List[String], and an ArrayBuffer isn't a List, so the compiler rejects return names. Call toList.
    • Arrays of different lengths. The loop follows firstNames.indices. If lastNames were shorter, lastNames(i) would throw an ArrayIndexOutOfBoundsException. The task gives matching arrays, so no check is needed here.
    • Order. The result keeps the order of the input, because the loop goes from index 0 upward.
    Keep going with ScalaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free