What To Buy
Part of the Fundamentals section of Coddy's Kotlin journey. Lesson 93 of 93.
Challenge
MediumWrite 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
containsorin - 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 buyalreadyBought(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
4Operators Part 1
Arithmetic OperatorsModulo OperatorAugmented AssignmentComparison OperatorsRecap - Simple Math7Basic IO
Println FunctionString TemplatesReadLine InputType ConversionRecap - Years Until RetirementRecap - True or False10Functions
Declare A FunctionParameters And ArgumentsReturn ValuesNamed ArgumentsDefault ValuesSingle Expression FunctionsRecap - Sigma FunctionRecap - Validation Function2Variables
Val vs VarType InferenceNumbersStringBooleanNaming ConventionsRecap - Initialize Variables5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Logical Operators Part 3Ternary With If ExpressionRecap - Simple Logic8Bill Split Calculator
Welcome MessageGetting Input3Nullability
What Is Null SafetyNullable TypesSafe Call OperatorElvis OperatorFunction Challenge BasicsNot Null AssertionRecap - Safe Access6Decision Making
If StatementIf - ElseIf As An ExpressionWhen ExpressionWhen With RangesRecap - Simple Calculator9Loops
For LoopWhile LoopDo-While LoopBreakContinueRanges In LoopsNested LoopRecap - FactorialRecap - Dynamic InputPractice on your own: Kotlin playground