Bird
0
0

You have a collection of integers and want to remove all even numbers efficiently. Which collection type and method combination is best in C#?

hard🚀 Application Q9 of 15
C Sharp (C#) - Collections
You have a collection of integers and want to remove all even numbers efficiently. Which collection type and method combination is best in C#?
AUse an array and loop to create a new array without even numbers.
BUse a List<int> and call RemoveAll with a predicate for even numbers.
CUse a Queue<int> and dequeue all odd numbers.
DUse a Stack<int> and pop even numbers.
Step-by-Step Solution
Solution:
  1. Step 1: Understand collection capabilities

    List supports RemoveAll method which removes elements matching a condition efficiently.
  2. Step 2: Compare other options

    Arrays require manual copying; Queue and Stack do not support direct removal by condition.
  3. Final Answer:

    Use a List and call RemoveAll with a predicate for even numbers. -> Option B
  4. Quick Check:

    Efficient removal by condition = C [OK]
Quick Trick: Use List.RemoveAll for condition-based removals [OK]
Common Mistakes:
MISTAKES
  • Trying to remove items from arrays directly
  • Misusing Queue or Stack for filtering
  • Manually copying arrays inefficiently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes