Review this generated collection code before it indexes a product catalog.
Return an alphabetical index of at most two active products without mutating the input, and reject duplicate SKUs.
kotlin
data class Product(
val sku: String,
val name: String,
val active: Boolean,
)
fun activeIndex(products: MutableList<Product>): Map<String, Product> {
val sorted = products
sorted.sortBy { it.name }
val selected = mutableListOf<Product>()
for (index in sorted.indices) {
val product = sorted.drop(index).first()
if (!product.active) continue
selected += product
if (selected.size == 2) break
}
return selected.associateBy { it.sku }
}
generated code is illustrative, not from any one model