Open In App

C# | How to get the last occurrence of the element in the List that match the specified conditions

Improve
Improve
Like Article
Like
Save
Share
Report

List<T>.FindLast(Predicate<T>) Method is used to search for an element which matches the conditions defined by the specified predicate and it returns the last occurrence of that element within the entire List<T>. 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.

Syntax:

public T FindLast (Predicate<T> match);

Parameter:

match: It is the Predicate<T> delegate which defines the conditions of the element which is to be searched.

Return Value: If the element found then this method will return the last element that matches the conditions defined by the specified predicate otherwise it returns the default value for type T. Exception: This method will give ArgumentNullException if the match is null. Below programs illustrate the use of List<T>.FindLast(Predicate<T>) Method: Example 1: 

CSharp




// C# Program to get the last occurrence
// of the element that match the specified
// conditions defined by the predicate
using System;
using System.Collections;
using System.Collections.Generic;
 
class Geeks {
 
    // function which checks whether an
    // element is even or not. Or you can
    // say it is the specified condition
    private static bool isEven(int i)
    {
        return ((i % 2) == 0);
    }
 
// Main Method
public static void Main(String[] args)
{
 
    // Creating an List<T> of Integers
    List<int> firstlist = new List<int>();
 
    // Adding elements to List
    firstlist.Add(4);
    firstlist.Add(2);
    firstlist.Add(7);
    firstlist.Add(2);
    firstlist.Add(6);
    firstlist.Add(4);
    firstlist.Add(3);
 
    Console.WriteLine("Elements Present in List:\n");
 
    int p = 0;
 
    // Displaying the elements of List
    foreach(int k in firstlist)
    {
        Console.Write("At Position {0}: ", p);
        Console.WriteLine(k);
        p++;
    }
 
    Console.WriteLine(" ");
 
    // Will give the last occurrence of the
    // element of firstlist that match the
    // conditions defined by predicate
    Console.Write("Index of Last element that fulfill the conditions: ");
    Console.WriteLine(firstlist.LastIndexOf(firstlist.FindLast(isEven)));
    Console.Write("Element is: ");
    Console.Write((firstlist.FindLast(isEven)));
}
}


Output:

Elements Present in List:

At Position 0: 4
At Position 1: 2
At Position 2: 7
At Position 3: 2
At Position 4: 6
At Position 5: 4
At Position 6: 3
 
Index of Last element that fulfill the conditions: 5
Element is: 4

Example 2: 

CSharp




// C# Program to get the last occurrence
// of the element that match the specified
// conditions defined by the predicate
using System;
using System.Collections;
using System.Collections.Generic;
 
class Geeks {
 
    // function which checks whether an
    // element is even or not. Or you can
    // say it is the specified condition
    private static bool isEven(int i)
    {
        return ((i % 2) == 0);
    }
 
// Main Method
public static void Main(String[] args)
{
 
    // Creating an List<T> of Integers
    List<int> firstlist = new List<int>();
 
    // Adding elements to List
    firstlist.Add(17);
    firstlist.Add(19);
    firstlist.Add(21);
    firstlist.Add(9);
    firstlist.Add(75);
    firstlist.Add(19);
    firstlist.Add(73);
 
    Console.WriteLine("Elements Present in List:\n");
 
    int p = 0;
 
    // Displaying the elements of List
    foreach(int k in firstlist)
    {
        Console.Write("At Position {0}: ", p);
        Console.WriteLine(k);
        p++;
    }
 
    Console.WriteLine(" ");
 
    // it will return index -1 as no element
    // found in the list which specified
    // the conditions and so element value
    // will be 0 as the list contains Integers
    Console.Write("Index of Last element that fulfill the conditions: ");
    Console.WriteLine(firstlist.LastIndexOf(firstlist.FindLast(isEven)));
    Console.Write("Element is: ");
    Console.Write((firstlist.FindLast(isEven)));
}
}


Output:

Elements Present in List:

At Position 0: 17
At Position 1: 19
At Position 2: 21
At Position 3: 9
At Position 4: 75
At Position 5: 19
At Position 6: 73
 
Index of Last element that fulfill the conditions: -1
Element is: 0

Time complexity: O(n) for FindLast method

Auxiliary Space: O(n) where n is the size of the list

Reference:



Last Updated : 20 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads