C# | How to check whether a List contains the elements that match the specified conditions
List<T>.Exists(Predicate<T>) Method is used to check whether the List<T> contains elements which match the conditions defined by the specified predicate. Properties of List:
- It is different from the arrays. A list can be resized dynamically but arrays cannot.
- List class can accept null as a valid value for reference types and it also allows duplicate elements.
- If the Count becomes equals to Capacity then the capacity of the List increases automatically by reallocating the internal array. The existing elements will be copied to the new array before the addition of the new element.
public bool Exists (Predicate<T> match);
match: It is the Predicate<T> delegate which defines the conditions of the elements to search for.
Return Value: This method returns True if the List<T> contains one or more elements that match the conditions defined by the specified predicate otherwise it returns False. Exception: This method will give ArgumentNullException if the match is null. Below programs illustrate the use List<T>.Exists(Predicate<T>) Method: Example 1:
Elements Present in List: 1 2 3 4 5 6 7 8 9 10 Result: True
Elements Present in List: 5 7 9 11 3 17 19 Result: False
Time complexity: O(n) for Exists method
Space complexity: O(n) where n is the size of the list
My Personal Notes arrow_drop_up
Please Login to comment...