Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

377
Views
Kotlin: la forma más efectiva de encontrar el primer índice del elemento mínimo en alguna lista de algunos objetos

Tengo una lista de instancias de alguna clase personalizada:

 data class Flight(val duration: Int)

Por ejemplo:

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

¿Cómo encontrar de manera más efectiva el primer índice del elemento mínimo en esta lista? En ese caso, el primer índice del elemento mínimo es 1, porque vuelos[1].duración = 5.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Algo como esto sería "más eficiente", supongo:

 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 }

Y este hace básicamente lo mismo y se ve mucho mejor:

 flights.withIndex().minBy { (_, f) -> f.duration }?.index
over 4 years ago · Santiago Trujillo Report

0

Con minBy() para obtener el elemento de la lista con la duración mínima
y luego indexOf() para obtener su índice:

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

Para solo 1 escaneo de la lista, puede hacer un bucle clásico:

 var index = if (flights.isEmpty()) -1 else 0 flights.forEachIndexed { i, flight -> if (flight.duration < flights[index].duration) index = i }
over 4 years ago · Santiago Trujillo Report

0

Tratar

 val index = flights.minBy { it.duration }?.let { flights.indexOf(it) }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!