grolegor
grolegor

Reputation: 1330

Kotlin: the most effective way to find first index of minimum element in some list of some objects

I have some list of instances of some custom class:

data class Flight(val duration: Int)

For example:

val flights = listOf(Flight(10), Flight(5), Flight(5), Flight(15), Flight(20))

How to most effectively find first index of the minimum element in this list? In that case the first index of min element is 1, because flights[1].duration = 5.

Upvotes: 9

Views: 8776

Answers (4)

ksnortum
ksnortum

Reputation: 3057

As of Kotlin 1.6 at least, minBy is deprecated. Use minByOrNull:

data class Flight(val duration: Int)
val flights = listOf(Flight(10), Flight(5), Flight(5), Flight(15), Flight(20))
val minIndex = flights.withIndex().minByOrNull { (_, f) -> f.duration }?.index ?: throw IndexOutOfBoundsException()
//                                 ^^^^^^^^^^^                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
println(minIndex)

Upvotes: 1

TomH
TomH

Reputation: 2719

Try

val index = flights.minBy { it.duration }?.let { flights.indexOf(it) }

Upvotes: 1

forpas
forpas

Reputation: 164099

With minBy() to get the list item with the minimum duration
and then indexOf() to get its index:

val index = flights.indexOf(flights.minBy { it.duration })

For just 1 scan of the list, you can do a classic loop:

var index = if (flights.isEmpty()) -1 else 0
flights.forEachIndexed { i, flight ->
    if (flight.duration < flights[index].duration) index = i
}

Upvotes: 6

s1m0nw1
s1m0nw1

Reputation: 81929

Something like this would be "most efficient", I guess:

var min: Pair<Int, Flight>? = null
for (f in flights.withIndex()) {
    if (min == null || min.second.duration > f.value.duration) min = f.index to f.value
}

And this one does basically the same and looks much better:

flights.withIndex().minBy { (_, f) -> f.duration }?.index

Upvotes: 15

Related Questions