Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C# Data Types
  • C# Decision Making
  • C# Methods
  • C# Delegates
  • C# Constructors
  • C# Arrays
  • C# ArrayList
  • C# String
  • C# Tuple
  • C# Indexers
  • C# Interface
  • C# Multithreading
  • C# Exception
Open In App
Next Article:
C# | How to get all elements of a List that match the conditions specified by the predicate
Next article icon

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

Last Updated : 20 Jun, 2022
Comments
Improve
Suggest changes
Like Article
Like
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:

  • https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.findlast?view=netframework-4.7.2

Next Article
C# | How to get all elements of a List that match the conditions specified by the predicate

K

Kirti_Mangal
Improve
Article Tags :
  • C#
  • CSharp-method
  • CSharp-Generic-List
  • CSharp-Collections-Namespace
  • CSharp-Generic-Namespace

Similar Reads

  • C# | How to get all elements of a List that match the conditions specified by the predicate
    List<T>.FindAll(Predicate<T>) Method is used to get all the elements that 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 refe
    3 min read
  • C# | First occurrence in the List that matches the specified conditions
    List<T>.Find(Predicate<T>) Method is used to search for an element which matches the conditions defined by the specified predicate and it returns the first occurrence of that element within the entire List<T>. Properties of List: It is different from the arrays. A list can be resiz
    3 min read
  • C# | Find the last node in LinkedList<T> containing the specified value
    LinkedList<T>.FindLast(T) method is used to find the last node that contains the specified value. Syntax: public System.Collections.Generic.LinkedListNode<T> FindLast (T value); Here, value is the value to locate in the LinkedList. Return Value: This method returns the last LinkedListNod
    2 min read
  • C# | Remove all elements of a List that match the conditions defined by the predicate
    List<T>.RemoveAll(Predicate<T>) Method is used to remove all the elements that 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 fo
    3 min read
  • C# | Check if every List element matches the predicate conditions
    List<T>.TrueForAll(Predicate<T>) is used to check whether every element in the List<T> matches the conditions defined by the specified predicate or not. Syntax: public bool TrueForAll (Predicate<T> match); Parameter: match: It is the Predicate<T> delegate which defines
    2 min read
  • C# | Find the first node in LinkedList<T> containing the specified value
    LinkedList<T>.Find(T) method is used to find the first node that contains the specified value. Syntax: public System.Collections.Generic.LinkedListNode<T> Find (T value); Here, value is the value to locate in the LinkedList. Return Value: This method returns the first LinkedListNode<T
    2 min read
  • C# | Removing the specified element from the List
    List.Remove(T) Method is used to remove the first occurrence of a specific object from the List. 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 elem
    2 min read
  • C# | Get the last node of the LinkedList<T>
    LinkedList<T>.Last property is used to get the last node of the LinkedList<T>. Syntax: public System.Collections.Generic.LinkedListNode Last { get; } Return Value: The last LinkedListNode<T> of the LinkedList<T>. Below given are some examples to understand the implementation
    2 min read
  • C# | Check if the SortedSet contains a specific element
    SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. SortedSet<T>.Contains(T) Method is used to check if a SortedSet contains a specific element or not. Properties: In C#, SortedSet class can be used to store, remov
    2 min read
  • C# | Remove elements from a SortedSet that match the predicate
    SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. SortedSet<T>.RemoveWhere(Predicate<T>) Method is used to remove all elements that match the conditions defined by the specified predicate from a SortedSet<T
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences