HI all - I would really appreciate some guidance on extracting values and/or indexes from a List. I'm working on some of the hyperskill courses and one question is returning the index of a given value - which may be duplicated- from a list.
Given the following list:
val products = listOf("milk", "eggs", "bread", "cheese", "eggs", "greens")
return the index(es) of a given string; e.g. "eggs" should return
1 4
. After some experimenting I've been trying to use forEachIndexed(), but I don't know how to express 'print $index where $value == "eggs"'. Can someone provide any pointers? Alternate approaches are welcome, of course π
Thanks in advance for your help!
val products = listOf("milk", "eggs", "bread", "cheese", "eggs", "greens")
products.forEachIndexed { index, s -> println("${s} ${index}") }
products.forEachIndexed { index, s -> ??? }