'How do we know which element is minimum in loop kotlin
Hey I have list in kotlin. I am iterating over it. I want to store true/false as flag index which one is minimum value in whole list. I tried some code, but it's not working.
fun main() {
val list = mutableListOf(4.83, 4.39, 3.58, 3.50, 3.46)
val minValue = mutableListOf<BestMinimumValue>()
var previousValue = 0.0
list.forEach {
minValue.add(BestMinimumValue(compareValue = previousValue > it))
previousValue = it
}
minValue.forEach {
println(it)
}
}
data class BestMinimumValue(
val compareValue: Boolean
)
Actual Output
BestMinimumValue(compareValue=false)
BestMinimumValue(compareValue=true)
BestMinimumValue(compareValue=true)
BestMinimumValue(compareValue=true)
BestMinimumValue(compareValue=true)
I'll explain what I need. In my list 3.46 is minimum value so on that place I need the flag as true and other one will be false.
Expected Output
BestMinimumValue(compareValue=false)
BestMinimumValue(compareValue=false)
BestMinimumValue(compareValue=false)
BestMinimumValue(compareValue=false)
BestMinimumValue(compareValue=true)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
