Menu
Coddy logo textTech

Pair And Triple

Part of the Fundamentals section of Coddy's Kotlin journey. Lesson 71 of 93.

Sometimes you need to group just two or three related values together without creating a full list. Kotlin provides Pair and Triple for exactly this purpose.

A Pair holds exactly two values. Create one using the Pair() constructor or the to keyword:

val coordinates = Pair(10, 20)
val person = "Alice" to 25  // Same as Pair("Alice", 25)

Access the values using first and second:

val point = Pair(5, 8)
println(point.first)   // 5
println(point.second)  // 8

A Triple works similarly but holds three values, accessed with first, second, and third:

val rgb = Triple(255, 128, 0)
println(rgb.first)   // 255
println(rgb.second)  // 128
println(rgb.third)   // 0

These are useful when a function needs to return multiple values, or when you want to group related data without defining a custom class. The values can be of different types, making them flexible for various situations.

challenge icon

Challenge

Medium

Write a function getMinMax that takes a list of integers and returns a Pair containing the minimum and maximum values, then formats them as a string.

Create a Pair where first holds the minimum value and second holds the maximum value from the list. Then return a formatted string using the pair's properties.

Parameters:

  • numbers (List<Int>): A list of integers with at least one element

Returns: A string in the format "Min: X, Max: Y" where X is the minimum value (first) and Y is the maximum value (second) from the Pair.

Input constraints: The input list is nonempty.

Try it yourself

fun getMinMax(numbers: List<Int>): String {
    // Write code here
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals

Practice on your own: Kotlin playground