Tutorialspoint
Problem
Solution
Submissions

LINQ Filter For a List of Greater Integers

Certification: Intermediate Level Accuracy: 60% Submissions: 5 Points: 10

Write a C# program to implement a method that uses LINQ to filter a list of integers greater than a specified value.

Example 1
  • Input:
    • List<int> numbers = new List<int> { 30, 65, 20, 80, 45, 90, 10 };
    • int threshold = 50;
  • Output:
    • [65, 80, 90]
  • Explanation:
    • Step 1: Check if the input list is null. It's not, so continue.
    • Step 2: Filter the list [30, 65, 20, 80, 45, 90, 10] for numbers greater than 50.
    • Step 3: The filtered list contains [65, 80, 90].
    • Step 4: Sort the list in ascending order, which is already [65, 80, 90].
    • Step 5: Return the filtered and sorted list.
Example 2
  • Input:
    • List<int> numbers = new List<int> { 5, 10, 15, 20, 25 };
    • int threshold = 50;
  • Output:
    • []
  • Explanation:
    • Step 1: Check if the input list is null. It's not, so continue.
    • Step 2: Filter the list [5, 10, 15, 20, 25] for numbers greater than 50.
    • Step 3: None of the values are greater than 50.
    • Step 4: Return an empty list [].
Constraints
  • 0 ≤ numbers.Count ≤ 10^5
  • -10^9 ≤ numbers[i] ≤ 10^9
  • -10^9 ≤ threshold ≤ 10^9
  • Time Complexity: O(n log n) due to sorting
  • Space Complexity: O(n) for the filtered list
Functions / MethodsListAdobeSwiggy
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Use LINQ's `Where()` method to filter elements based on the condition
  • Apply `OrderBy()` to sort the filtered elements
  • Use `ToList()` to convert the LINQ query result to a List
  • Add null checks and empty list validation at the beginning
  • For large datasets, consider optimizing the LINQ query execution

Steps to solve by this approach:

 Step 1: Validate input by checking if the list is null.
 Step 2: Use LINQ's Where() method to filter elements greater than the threshold.
 Step 3: Apply OrderBy() to sort the filtered elements in ascending order.
 Step 4: Convert the result to a List using ToList().
 Step 5: Return the filtered and sorted list.

Submitted Code :