Skip to main content

How to find the range of elements in an array in Kotlin

How to find the range of elements in an array in Kotlin.

Here's a detailed step-by-step tutorial on how to find the range of elements in an array in Kotlin.

Step 1: Create an Array

First, we need to create an array in Kotlin. An array is a collection of elements of the same type. We can create an array using the arrayOf function and pass the elements as arguments.

val array = arrayOf(5, 10, 15, 20, 25)

In this example, we have created an array of integers with five elements.

Step 2: Find the Minimum and Maximum Elements

To find the range of elements in the array, we need to find the minimum and maximum elements. Kotlin provides a variety of ways to find the minimum and maximum values in an array.

Method 1: Using the min() and max() Functions

The min() and max() functions in Kotlin can be used to find the minimum and maximum values in an array, respectively. These functions return the smallest and largest element in the array.

val minElement = array.min()
val maxElement = array.max()

In this example, minElement will have the value 5, and maxElement will have the value 25.

Method 2: Using the minOrNull() and maxOrNull() Functions

Starting from Kotlin 1.3, the min() and max() functions are deprecated. Instead, you can use the minOrNull() and maxOrNull() functions. These functions return the smallest and largest element in the array, or null if the array is empty.

val minElement = array.minOrNull()
val maxElement = array.maxOrNull()

In this example, minElement will have the value 5, and maxElement will have the value 25.

Method 3: Using a Loop

If you prefer a more manual approach, you can use a loop to find the minimum and maximum elements in the array. You can initialize the minimum and maximum variables with the first element of the array and iterate over the remaining elements, updating the minimum and maximum values if necessary.

var minElement = array[0]
var maxElement = array[0]

for (i in 1 until array.size) {
if (array[i] < minElement) {
minElement = array[i]
}
if (array[i] > maxElement) {
maxElement = array[i]
}
}

In this example, minElement will have the value 5, and maxElement will have the value 25.

Step 3: Calculate the Range

Once we have the minimum and maximum elements, we can calculate the range by subtracting the minimum from the maximum.

val range = maxElement - minElement

In this example, range will have the value 20.

Complete Example

Here's the complete example that puts everything together:

val array = arrayOf(5, 10, 15, 20, 25)
val minElement = array.minOrNull()
val maxElement = array.maxOrNull()
val range = maxElement!! - minElement!!

println("Minimum Element: $minElement")
println("Maximum Element: $maxElement")
println("Range: $range")

When you run this code, you will see the following output:

Minimum Element: 5
Maximum Element: 25
Range: 20

That's it! You have successfully found the range of elements in an array in Kotlin.