Skip to content
Arrays and Tuples
step 1/5

Reading — step 1 of 5

Learn

~3 min readCollections

Arrays and Tuples

An array type in TypeScript is written T[] — "array of T" — and the compiler enforces it on every read and write.

Typed arrays

const nums: number[] = [1, 2, 3];
nums.push(4);      // OK
nums.push("five"); // compile error — "five" is not a number

The generic form Array<number> means exactly the same thing; T[] is the everyday convention. The payoff is that everything you derive from the array stays typed:

const doubled: number[] = nums.map(n => n * 2);       // still numbers
const total: number = nums.reduce((a, b) => a + b, 0); // one number

Tuples — fixed positions, fixed types

An array says "many values, one type." A tuple says "exactly these positions, each with its own type":

const point: [number, number] = [3, 4];
const entry: [string, number] = ["score", 95];

const [label, value] = entry; // label: string, value: number

Destructuring pulls the positions into named variables with the right types inferred. Tuples show up in real APIs constantly: Object.entries(obj) yields [string, V] pairs, and React's useState returns [value, setter].

Finding the maximum — the reduce idiom

reduce walks the array carrying an accumulator. For a maximum, the accumulator is "the biggest value seen so far":

const max = nums.reduce((max, n) => n > max ? n : max);

Notice there is NO second argument. When you omit the initial value, reduce seeds the accumulator with the FIRST element and starts iterating from the second — exactly what a max-scan wants, because the first element genuinely is the best answer so far.

(Math.max(...nums) also works for reasonably sized arrays: the spread turns the array into individual arguments.)

The trap: a "harmless" initial value

Beginners reflexively hand reduce a 0 to start with:

// WRONG for negative inputs:
const max = nums.reduce((max, n) => n > max ? n : max, 0);

Now 0 competes as if it were in the array. For [-3, -1, -7] every element loses to 0, and the "maximum" comes out as 0 — a value that is not even in the array. The rule: an initial value must be an identity for the operation (0 for sum, 1 for product). For max, the identity is -Infinity — or simply omit it and let the first element seed.

One caveat when omitting: reduce with no initial value throws on an empty array. This exercise guarantees at least one number, so seeding from the first element is the right call here.

Your exercise

The starter parses a line of space-separated integers:

const nums: number[] = require('fs').readFileSync(0, 'utf-8').trim().split(' ').map(Number);

Print the largest one, using .reduce((max, n) => n > max ? n : max).

The exact mistake the grader will catch: passing 0 as reduce's initial value. The hidden test feeds -3 -1 -7 and expects -1 — with a stray 0 seed you print 0 and fail. Omit the initial value (or use -Infinity), and print only the number.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…