Menu
Coddy logo textTech

What To Buy

Part of the Fundamentals section of Coddy's Kotlin journey. Lesson 93 of 93.

challenge icon

Challenge

Medium

Write a function whatToBuy that takes shoppingList and alreadyBought and returns a list of items that still need to be purchased.

Filter out items from the shopping list that have already been bought, keeping only the remaining items in their original order.

Logic:

  • Iterate through each item in the shopping list
  • Check if the item exists in the already bought list using contains or in
  • If the item is NOT in the already bought list, include it in the result
  • Maintain the original order from the shopping list

Parameters:

  • shoppingList (List<String>): Items you need to buy
  • alreadyBought (List<String>): Items you've already purchased

Returns: A list containing only the items that still need to be bought, in their original order (List<String>)

Try it yourself

fun whatToBuy(shoppingList: List<String>, alreadyBought: List<String>): List<String> {
    // Write code here
}

All lessons in Fundamentals

Practice on your own: Kotlin playground