Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

184
Vistas
Nested array indexOf()

Kotlin newbie here --

Is there a "neater" way to do this? (my int values are guaranteed unique across the entire set)

fun main() {
    val sect = arrayOf(arrayOf(1, 2, 3), arrayOf(4, 5), arrayOf(6, 7, 8, 9))
    val (i1, i2) = deepIndexOf(sect, 6)
    println("$i1 - $i2")
}
fun deepIndexOf(a: Array<Array<Int>>, x: Int): Pair<Int, Int> {
    for ((i1, sub) in a.withIndex()) {
        val i2 = sub.indexOf(x)
        if (i2 > -1) return i1 to i2
    }
    return -1 to -1
}
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

One thing that stands out directly is that you could create an extension function. It would look something like this:

fun Array<Array<Int>>.deepIndexOf(element: Int): Pair<Int, Int> {
    for ((i1, this) in a.withIndex()) 
             ...

Then the call would look a bit more "natural", kind of like the call to sub.indexOf() looks like:

val (i1, i2) = sect.deepIndexOf(6)

One thing to note, if you would use List instead of Array, then you could do fun Collection<Collection<Int>.etc. Then you would be able to call this function on Set as well. Heck, you could call it on a setOf(listOf(), setOf(), listOf()).

Another change might be to use Kotlin's forEach, making the first lines:

fun Array<Array<Int>>.deepIndexOf(element: Int): Pair<Int, Int> {
    withIndex().forEach { (i1, sub) ->
              ...

As for the if statement, it looks ok as it is, you might be able to change it to something a bit more functional, but it might look forced, since it doesn't really need it. We can use takeIf instead of the if, which would give us the input value if the predicate is true, or null. Then we can use ?.let as an "if" statement to return. But honestly that creates more steps.

withIndex().forEach { (i1, sub) ->
    sub.indexOf(element) //get the index
        .takeIf { it > -1 } //if index > -1 propagate the index, else null
        ?.let { return i1 to it } //if the above is not null, return
}

To achieve the same number of steps as your example, we would need to remove the ?.let and that would force us to create something that is really hard to read.

withIndex().forEach { (i1, sub) ->
    return i1 to (sub.indexOf(element).takeIf { it > -1 } ?: return@forEach)
}

This uses the elvis operator ?: to say that if the result of sub.indexOf(element).takeIf { it > -1 } is null, then continue with the next element of the loop.

over 4 years ago · Santiago Trujillo Denunciar

0

fun deepIndexOf(a: Array<Array<Int>>, x: Int): Pair<Int, Int> {
  return a
    .mapIndexed { index, ints -> index to ints.indexOf(x) }
    .firstOrNull { (_, found) -> found != -1 }
    ?: (-1 to -1)
}

Edit: based on @Alex.T's comment below, here is a solution which does not process the entire input list:

fun deepIndexOf(a: Array<Array<Int>>, x: Int): Pair<Int, Int> {
  return a
    .withIndex()
    .dropWhile { (_, ints) -> ints.indexOf(x) == -1 }
    .map { (index, ints) -> index to ints.indexOf(x) }
    .getOrElse(0) { -1 to -1 }
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda