Start learning free

LESSON 266 OF 675 · TUPLES, SORTING & BUILT-INS

Oldest Pet in TypeScript: Looping Over 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

    TYPESCRIPT · TUPLES, SORTING & BUILT-INS

    Oldest Pet in TypeScript, explained

    Problem overview

    The method oldestPet gets an array pets. Each pet is a tuple [string, number]: its name and its age. The method returns the name of the oldest pet. If several pets share the oldest age, the one that comes first in pets wins. The array 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 comes first. For [["Kiki", 0]] it is "Kiki".

    Approach

    Remember the oldest pet seen so far and compare each pet with it. Start with the first pet, because before the loop looks at anything else it is the oldest you have seen. When a pet is older, it replaces the remembered one. The remembered value is the whole pair, so the name comes along with the age, and the name is read only at the end.

    Why the tie goes to the first pet

    The comparison is strict: >, not >=. A pet of the same age doesn't replace the one you have, so on a tie the earlier pet stays. With >=, Milo would take Coco's place in the second example.

    Step by step

    1. Declare oldest as a [string, number] with let, set to pets[0].
    2. Loop over pets with for...of, naming each pair pet.
    3. In the loop, check whether pet[1] (this pet's age) is greater than oldest[1].
    4. If it is, set oldest = pet.
    5. After the loop, return oldest[0], the name.

    The solution in TypeScript

    class Solution {
        oldestPet(pets: [string, number][]): string {
            let oldest: [string, number] = pets[0];
            for (let pet of pets) {
                if (pet[1] > oldest[1]) {
                    oldest = pet;
                }
            }
            return oldest[0];
        }
    }
    

    The parameter type [string, number][] is an array of tuples. Index 0 of a pair is the name and index 1 is the age, so pet[1] > oldest[1] compares ages. Assigning oldest = pet doesn't copy the pair; both names point at the same array, which is fine because the code never changes it.

    The first pass of the loop compares pets[0] with itself. The strict comparison is false, so nothing changes.

    Tracing the second example

    Take [["Luna", 3], ["Coco", 9], ["Milo", 9]]. oldest starts as Luna's pair. Coco is 9 and 9 > 3, so Coco's pair replaces it. Milo is 9, but 9 > 9 is false, so Coco stays. The method returns "Coco".

    Nothing is sorted or copied: the loop visits every pet once, so the time grows in step with the number of pets, and only one extra variable is kept.

    Testing

    petsResult
    [["Rex", 4], ["Bella", 7], ["Max", 2]]Bella
    [["Luna", 3], ["Coco", 9], ["Milo", 9]]Coco
    [["Kiki", 0]]Kiki

    These are the results of calling the method on the statement's examples.

    Common mistakes

    • Using >=. The last of the oldest pets wins instead of the first.
    • Starting oldest at an age of 0. Ages start at 0, so a lone pet of age 0 (Kiki) would have no name to return. Start from pets[0].
    • Mixing up the indexes. pet[0] is the name, a string; comparing names with > compiles but sorts alphabetically instead of by age.
    • Sorting first. Sorting the array by age would also work, but it changes the caller's array and needs a tie rule; one pass with a strict comparison is simpler.
    • Returning the pair. The method returns a string, so return oldest[0], not oldest.
    Keep going with TypeScript500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free