Start learning free

LESSON 266 OF 677 · PAIRS, SORTING & BUILT-INS

Oldest Pet in Kotlin: Scanning a List of Pairs

Practice Read the full explanation

You get an ArrayList of pets, pets. Each pet is a pair: its name and its age, like ("Rex", 4).

Return the name of the oldest pet.

Several pets may share the oldest age. Then return the one that comes first in pets.

The starter code shows how your language holds a pair.

 

Example 1:

Input: pets = [("Rex", 4), ("Bella", 7), ("Max", 2)]
Output: "Bella"
Explanation: Bella is 7.
Rex and Max are younger.

Example 2:

Input: pets = [("Luna", 3), ("Coco", 9), ("Milo", 9)]
Output: "Coco"
Explanation: Coco and Milo are both 9.
Coco comes first in the list.

Example 3:

Input: pets = [("Kiki", 0)]
Output: "Kiki"
Explanation: Kiki is the only pet.

 

Constraints:

  • pets has at least one pet.
  • Every age is a whole number from 0 to 30.
  • Each name is a capital letter followed by lowercase letters, like "Rex".
STEP 1 OF 5
    Ctrl + Enter to check

    KOTLIN · PAIRS, SORTING & BUILT-INS

    Oldest Pet in Kotlin, explained

    Problem overview

    oldestPet receives a list of pets. Each pet is a Pair<String, Int>: its name, then its age. The function returns the name of the oldest pet, as a String. If several pets share the oldest age, the one that comes first in the list wins.

    For [("Rex", 4), ("Bella", 7), ("Max", 2)] the answer is "Bella". For [("Luna", 3), ("Coco", 9), ("Milo", 9)] Coco and Milo are tied at 9, so the answer is "Coco". A list with one pet, [("Kiki", 0)], gives "Kiki". The list always has at least one pet.

    Approach

    Keep the best pet found so far in a variable, and compare every pet with it. When a pet is older, it replaces the variable. Store the whole pair, not only the age: the name has to come along, because it is what you return at the end.

    Start the variable at the first pet, pets[0]. That is a real pet and the oldest you have seen so far, before looking at any other. Starting at an age like 0 would need a made-up name.

    Step by step

    1. Declare oldest with var and set it to pets[0]. It is a var because it will be replaced.
    2. Loop over the pets with for (pet in pets).
    3. Inside, compare ages. A pair's age is its second value, so the test is pet.second > oldest.second.
    4. When the test is true, set oldest = pet.
    5. After the loop, return oldest.first, the name.

    The solution in Kotlin

    class Solution {
        fun oldestPet(pets: List<Pair<String, Int>>): String {
            var oldest = pets[0]
            for (pet in pets) {
                if (pet.second > oldest.second) {
                    oldest = pet
                }
            }
            return oldest.first
        }
    }
    

    On the three examples it returns:

    Bella
    Coco
    Kiki
    

    The comparison is strict (>, not >=). In the second example, when the loop reaches Milo, his age 9 is equal to Coco's 9, not greater, so Coco stays. With >= the last of the tied pets would win, and the answer would be Milo.

    The type Pair<String, Int> tells you what each slot holds: first is a String and second is an Int. Mixing them up is a compile error, since you can't return an Int where a String is expected.

    A trace of the tie

    For [("Luna", 3), ("Coco", 9), ("Milo", 9)], this is who is the oldest after each pet is looked at:

    Luna: oldest is Luna
    Coco: oldest is Coco
    Milo: oldest is Coco
    

    Luna replaces nothing (she is the start), Coco is older than Luna, and Milo is only equal to Coco, so Coco stays. Changing the test to >= makes the function return Milo for this list.

    The list is read once, so the work grows in step with the number of pets.

    Edge cases and common mistakes

    • Using >=. It breaks the tie rule.
    • Comparing the pair itself. pet > oldest doesn't compile: the compiler reports an unresolved reference to compareTo, because pairs have no order. Compare their second values.
    • Returning oldest. That is a pair, but the function's return type is String. Return oldest.first.
    • Storing only the age. Then the name is lost by the end of the loop.
    • A one-pet list works without a special case: the loop compares the pet with itself, nothing changes, and its name is returned.
    Keep going with Kotlin500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free