
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find the Last Matching Element in an Array Using C#
To find the last matching element, use the Array.LastIndexOf method. Returns -1 if the element isn’t present in the integer array.
The following is the array −
int[] val = { 97, 45, 76, 21, 89, 45 };
Now, let’s say you need to search the last Index of element 45. For that, use Array.LastIndexOf() method −
int res = Array.LastIndexOf(val, 45);
The following is an example −
Example
using System; using System.Text; public class Demo { public static void Main() { int[] val = { 97, 45, 76, 21, 89, 45 }; // last Index of element 45 int res = Array.LastIndexOf(val, 45); // Display the index Console.WriteLine("Index of element 45 is = "+res); } }
Output
Index of element 45 is = 5
Advertisements