min

@JvmName(name = "minOrThrow")
fun Sequence<Double>.min(): Double(source)
@JvmName(name = "minOrThrow")
fun Sequence<Float>.min(): Float(source)

Returns the smallest element.

If any of elements is NaN, this function returns NaN.

The operation is terminal.

Since Kotlin

1.7

Throws

if the sequence is empty.

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   // The largest and smallest elements in the array
val numbers = doubleArrayOf(3.0, 7.2, 2.4, 6.5)
println(numbers.max()) // 7.2
println(numbers.min()) // 2.4

// max() and min() return `NaN` if any of elements is `NaN`
val numbersWithNaN = doubleArrayOf(3.0, Double.NaN, 7.2, 2.4, 6.5)
println(numbersWithNaN.max()) // NaN
println(numbersWithNaN.min()) // NaN

val emptyArray = doubleArrayOf()

// max() and min() throw if the array is empty
// emptyArray.max() // will fail with NoSuchElementException
// emptyArray.min() // will fail with NoSuchElementException

// maxOrNull() and minOrNull() return null if the array is empty
println(emptyArray.maxOrNull()) // null
println(emptyArray.minOrNull()) // null 
   //sampleEnd
}

@JvmName(name = "minOrThrow")
fun <T : Comparable<T>> Sequence<T>.min(): T(source)

Returns the smallest element.

If there are multiple equal minimal elements, this function returns the first of those elements.

The operation is terminal.

Since Kotlin

1.7

Throws

if the sequence is empty.

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   // The largest and smallest elements according to String.compareTo
val names = listOf("Alice", "Bob", "Carol")
println(names.max()) // Carol
println(names.min()) // Alice

val emptyList = emptyList<Int>()

// max() and min() throw if the collection is empty
// emptyList.max() // will fail with NoSuchElementException
// emptyList.min() // will fail with NoSuchElementException

// maxOrNull() and minOrNull() return null if the collection is empty
println(emptyList.maxOrNull()) // null
println(emptyList.minOrNull()) // null 
   //sampleEnd
}

Deprecated

Warning since 1.4

Error since 1.5

Hidden since 1.6

Use minOrNull instead.

Replace with

this.minOrNull()

Since Kotlin

1.1

fun <T : Comparable<T>> Sequence<T>.min(): T?(source)

Deprecated

Warning since 1.4

Error since 1.5

Hidden since 1.6

Use minOrNull instead.

Replace with

this.minOrNull()

Since Kotlin

1.0