Start learning free

LESSON 265 OF 671 · SORTING & BUILT-INS

Oldest Pet in Go: Structs and a Strict Comparison

Practice Read the full explanation

You get a slice 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

    GO · SORTING & BUILT-INS

    Oldest Pet in Go, explained

    Problem overview

    The function oldestPet receives a slice of pets, pets, and returns the name of the oldest one. A pet is a Pet, a struct the lesson declares for you with a Name string and an Age int. The slice has at least one pet. If several pets share the oldest age, the one that comes first in the slice wins.

    For Rex (4), Bella (7) and Max (2) the answer is Bella. For Luna (3), Coco (9) and Milo (9) it is Coco, because Coco and Milo tie and Coco is earlier. A single pet, Kiki (0), is its own answer.

    Approach: keep the best pet so far

    This is the same running-best idea as finding a largest number, with one twist: you need the name, but you compare ages. The simplest way is to keep the whole pet. Start with the first pet as the oldest so far. Then look at every pet, and whenever one is strictly older than the current best, replace the best with it. At the end, return the best pet's name.

    The tie rule is handled by the comparison. Using > means a pet of equal age never replaces the current one, so the earliest of the oldest stays. With >= the later one would win: on Luna, Coco and Milo that version returns Milo.

    Step by step

    1. Start with pets[0] in oldest. It is the oldest you have seen before looking at anyone else, and it makes the empty-start problem disappear.
    2. Loop with for _, pet := range pets. The underscore drops the index.
    3. Compare ages with pet.Age > oldest.Age. Fields are read with a dot.
    4. Replace the best with oldest = pet. Assigning a struct copies all its fields, so the name arrives together with the age.
    5. Return oldest.Name: the function gives back only the name, not the whole pet.

    The solution in Go

    func oldestPet(pets []Pet) string {
        oldest := pets[0]
        for _, pet := range pets {
            if pet.Age > oldest.Age {
                oldest = pet
            }
        }
        return oldest.Name
    }

    The lesson declares the struct as type Pet struct { Name string; Age int }, so you don't write it in the editor. Go has no pair type: a small struct with named fields plays that role, and pet.Name reads better than a position number would. In your own program you can build the examples with a composite literal, such as []Pet{{"Rex", 4}, {"Bella", 7}, {"Max", 2}}.

    The loop variable pet is a copy of each element, which is fine here because the function only reads it. The first pass compares pets[0] with itself, which changes nothing.

    Testing

    Calling oldestPet on the statement's three examples prints:

    Bella
    Coco
    Kiki

    Edge cases and common mistakes

    • Using >=. Ties then go to the last pet instead of the first.
    • Returning the age or the pet. The return type is string, so the function must return oldest.Name.
    • Keeping only the age. If you track just the largest age, you lose the name that goes with it. Keeping the whole pet avoids two variables that must be updated together.
    • Starting from age 0. It works only because ages begin at 0 and still needs a name variable; starting from pets[0] needs no assumption about the data.
    Keep going with Go500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free