Bird
0
0

What happens if you use OrderBy on a collection with duplicate keys? For example:

hard🧠 Conceptual Q10 of 15
C Sharp (C#) - LINQ Fundamentals
What happens if you use OrderBy on a collection with duplicate keys? For example:
var nums = new List<int> { 2, 3, 2, 1 };
var sorted = nums.OrderBy(n => n);
AOrderBy throws an exception on duplicates
BDuplicates are removed automatically
CDuplicates are preserved and appear in sorted order
DOnly the first occurrence of each key is kept
Step-by-Step Solution
Solution:
  1. Step 1: Understand OrderBy behavior with duplicates

    OrderBy sorts all elements, including duplicates, preserving them.
  2. Step 2: Confirm duplicates are not removed

    Duplicates remain in the sorted sequence; no elements are removed.
  3. Final Answer:

    Duplicates are preserved and appear in sorted order -> Option C
  4. Quick Check:

    OrderBy keeps duplicates, sorts all elements [OK]
Quick Trick: OrderBy sorts all elements, duplicates stay [OK]
Common Mistakes:
MISTAKES
  • Thinking duplicates are removed
  • Assuming OrderBy throws error on duplicates
  • Confusing OrderBy with Distinct

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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