This article explores different ways to find the minimum and maximum element in an array in Kotlin.

1. Using toList() function

The idea is to convert the array into a list and call the min() and max() functions of the List interface to get the minimum and maximum element.

Download Code

2. Using map() function

Here, the idea is to get a list of valid indices for the array and transform the indices into the corresponding element in the array using the map() function. Then you can call the max() and min() function to get the minimum and maximum element, respectively.

Download Code

3. Using Sorting

Another plausible way is to sort the array in ascending order. Then the minimum and maximum elements would be the first and last array elements, respectively. However, this approach modifies the original array and is not preferred for large arrays.

Download Code

4. Using summaryStatistics() function

To get the minimum and maximum element, you can also call the min and max properties on a IntSummaryStatistics object, which provides summary data about the elements of a stream.

Download Code

5. Custom Routine

Finally, you can also write your own routine for finding the minimum and maximum element in the array:

Download Code

That’s all about finding the minimum and maximum elements in an array in Kotlin.