Start learning free

LESSON 267 OF 678 · TUPLES, SORTING & BUILT-INS

Oldest Pet in Swift: Finding the Maximum in an Array of Tuples

Practice Read the full explanation

You get an array 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

    SWIFT · TUPLES, SORTING & BUILT-INS

    Oldest Pet in Swift, explained

    Problem overview

    The function oldestPet receives an array pets. Each pet is a tuple (name: String, age: Int). It returns the name of the oldest pet. When several pets share the oldest age, the one that comes first in the array wins. The array always has at least one pet.

    For [("Rex", 4), ("Bella", 7), ("Max", 2)] the answer is "Bella". For [("Luna", 3), ("Coco", 9), ("Milo", 9)] it is "Coco", because Coco and Milo are both 9 and Coco is first.

    Approach

    This is a running maximum, like finding the largest number in an array, except that the value kept is the whole pair. Start with the first pet as the oldest so far. Then look at each pet: if it is older than the one you keep, keep it instead. At the end, return the kept pet's name.

    The tie rule comes from the comparison. With a strict "older than" (>), a pet of the same age doesn't replace the current one, so on a tie the first pet stays. Writing >= would make the last pet of the oldest age win, which is wrong here.

    Here is the loop on the second example. The table shows which pet is kept as oldest after each one is handled.

    petoldest after this pet
    LunaLuna
    CocoCoco
    MiloCoco

    Coco (9) is older than Luna (3), so Coco replaces her. Milo is also 9, which is not older than 9, so Coco stays. This is the tie rule at work.

    Step by step

    1. Declare oldest with var, starting at pets[0]. The first pet is the oldest you've seen before the loop looks at the others.
    2. Loop over pets with for pet in pets.
    3. Compare ages: pet.age > oldest.age.
    4. Replace the whole pair: oldest = pet. Keeping the pair brings the name along with the age.
    5. Return oldest.name after the loop. The function returns only the name.

    The solution in Swift

    class Solution {
        func oldestPet(_ pets: [(name: String, age: Int)]) -> String {
            var oldest = pets[0]
            for pet in pets {
                if pet.age > oldest.age {
                    oldest = pet
                }
            }
            return oldest.name
        }
    }
    

    Swift details:

    • An array of labelled tuples. The parameter type [(name: String, age: Int)] gives each pair its labels, so inside the loop pet.name and pet.age read the parts. Callers can pass plain literals such as [("Rex", 4), ("Bella", 7)].
    • Tuples are values. oldest = pet copies the pair. Nothing in pets is changed.
    • The first pet is compared with itself. The loop starts at the first element, so pet.age > oldest.age is false there. That is harmless.

    Edge cases and common mistakes

    • One pet. [("Kiki", 0)] returns "Kiki".
    • Ties. For Luna 3, Coco 9, Milo 9 the function returns "Coco". With >= it would return "Milo".
    • Starting with an age of 0. A pet of age 0 is allowed, and an "oldest age so far" variable starting at 0 would never record such a pet when it is the only one. Starting from pets[0] avoids this.
    • Returning the pair or the age. The function must return the name, oldest.name.
    • Comparing names. pet.name > oldest.name compiles, because strings can be compared, but it finds the alphabetically last name, not the oldest pet.
    • An empty array. pets[0] would stop the program with an index-out-of-range error. The constraints promise at least one pet.

    Testing

    • [("Rex", 4), ("Bella", 7), ("Max", 2)] gives "Bella".
    • [("Luna", 3), ("Coco", 9), ("Milo", 9)] gives "Coco".
    • [("Kiki", 0)] gives "Kiki".
    Keep going with Swift500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free