Array.LastIndexOf Method in C# | Set - 1
Last Updated :
12 Mar, 2019
Array.LastIndexOf method is used to find the last matching element in a one-dimensional array. It starts the search from the last element of an array. It returns the index of the element that contains the specified value. There are 6 methods in the overload list of this method as follows:
- LastIndexOf(Array, Object)
- LastIndexOf(Array, Object, Int32)
- LastIndexOf(Array, Object, Int32, Int32)
- LastIndexOf<T>(T[], T)
- LastIndexOf<T>(T[], T, Int32)
- LastIndexOf<T>(T[], T, Int32, Int32)
Here we discuss only the first three methods.
LastIndexOf(Array, Object)
This method searches for the specified object and returns the index of the last occurrence within the entire one-dimensional Array.
Syntax: public static int LastIndexOf (Array arr, object value);
Parameters:
arr: It is the one-dimensional Array to search.
value: It is the object to locate in array.
Return Value: If the index of
value is found then, this method returns the index of the last occurrence of that
value within the entire array. If not found then, the lower bound of the array minus 1.
Exceptions:
- ArgumentNullException: If the array arr is null.
- RankException: If the array arr is multidimensional.
Example 1:
csharp
// C# program to demonstrate the
// Array.LastIndexOf(Array,
// Object) method
using System;
class GFG {
static void Main()
{
int[] arr = {1, 2, 6, 8, 6, 2};
// search for "6" from end of the array
// returns index 4
int indx1 = Array.LastIndexOf(arr, 6);
Console.WriteLine("First Index of 6 search"+
" from end found at " + indx1);
// search for "2" from end of the array
// returns index 5
int indx2 = Array.LastIndexOf(arr, 2);
// when the object is found then
// the search will stop searching
Console.WriteLine("First Index of 2 search "+
"from end found at " + indx2);
}
}
Output:
First Index of 6 search from end found at 4
First Index of 2 search from end found at 5
Example 2:
csharp
// C# program to demonstrate the
// Array.LastIndexOf(Array,
// Object) method
using System;
class GFG {
// Main Method
public static void Main()
{
// creating a array of type Array
Array arr = Array.CreateInstance(typeof(String), 8);
// 0, 1, 2, 3, 4, 5, 6, 7 are
// the indexes of the elements
arr.SetValue("java", 0);
arr.SetValue("C#", 1);
arr.SetValue("C++", 2);
arr.SetValue("C", 3);
arr.SetValue("Python", 4);
arr.SetValue("C#", 5);
arr.SetValue("C++", 6);
arr.SetValue("Ruby", 7);
String s1 = "C#";
int index1 = Array.LastIndexOf(arr, s1);
Console.WriteLine("First Index of C# search"+
" from end found at " + index1);
String s2 = "C++";
int index2 = Array.LastIndexOf(arr, s2);
Console.WriteLine("First Index of C++ search"+
" from end found at " + index2);
}
}
Output:
First Index of C# search from end found at 5
First Index of C++ search from end found at 6
LastIndexOf(Array, Object, Int32)
This method searches for the specified object and returns the index of the last occurrence within the range of elements in the one-dimensional Array that extends from the first element to the specified index.
Syntax: public static int LastIndexOf (Array arr, object value, int start);
Parameters:
arr: It is the one-dimensional Array to search.
value: It is the object to locate in array.
start: It is the starting index of the backward search.
Return Value: If the index of
value is found then, this method returns the index of the last occurrence of that
value within a range of elements in array
"arr that extends from the first element to start. If not found then, the lower bound of the array minus 1.
Exceptions:
- ArgumentNullException: If the array arr is null.
- RankException: If the array arr is multidimensional.
- ArgumentOutOfRangeException: If "start" index is outside the range of valid indexes for array.
Example 1:
csharp
// C# program to demonstrate the
// Array.LastIndexOf(Array, Object,
// Int32) method
using System;
class GFG {
static void Main()
{
int[] arr = {1, 2, 6, 8, 6, 2};
// search for "6"
// search start from index 3
// so returns index 2
int indx1 = Array.LastIndexOf(arr, 6, 3);
Console.WriteLine("First Index of 6 search "+
"from index 3 found at " + indx1);
// search for "2"
// search start from index 2
// so returns index 1
int indx2 = Array.LastIndexOf(arr, 2, 2);
Console.WriteLine("First Index of 2 search "+
"from index 2 found at " + indx2);
}
}
Output:
First Index of 6 search from index 3 found at 2
First Index of 2 search from index 2 found at 1
Example 2:
csharp
// C# program to demonstrate the
// Array.LastIndexOf(Array, Object,
// Int32) method
using System;
class GFG {
// Main Method
public static void Main()
{
// creating a array of type Array
Array arr = Array.CreateInstance(typeof(String), 8);
// 0, 1, 2, 3, 4, 5, 6, 7 are the
// indexes of the elements
arr.SetValue("java", 0);
arr.SetValue("C#", 1);
arr.SetValue("C++", 2);
arr.SetValue("C", 3);
arr.SetValue("Python", 4);
arr.SetValue("C#", 5);
arr.SetValue("C++", 6);
arr.SetValue("Ruby", 7);
String s1 = "C#";
// the search start from index 4
// so returns index 1
int index1 = Array.LastIndexOf(arr, s1, 4);
Console.WriteLine("First Index of C# search "+
"from index 4 found at " + index1);
String s2 = "C++";
// the search start from index 4
// so returns index 2
int index2 = Array.LastIndexOf(arr, s2, 3);
Console.WriteLine("First Index of C++ search"+
" from index 3 found at " + index2);
}
}
Output:
First Index of C# search from index 4 found at 1
First Index of C++ search from index 3 found at 2
LastIndexOf(Array, Object, Int32, Int32)
This method searches for the specified object and returns the index of the last occurrence within the range of elements in the one-dimensional array that contains the specified number of elements and ends at the specified index.
Syntax: public static int LastIndexOf (Array arr, object value, int start, int count);
Parameters:
arr: It is the one-dimensional Array to search.
value: It is the object to locate in array.
start: It is the starting index of the search.
count: It is the number of elements in the section to search.
Return Value: If the index of
value is found then, this method returns the index of the last occurrence of that
value within a range of elements in array
"arr" that contains the number of elements specified in
"count" and ends at
"start" index. If not found then, the lower bound of the array minus 1.
Exceptions:
- ArgumentNullException: If the array arr is null.
- RankException: If the array arr is multidimensional.
- ArgumentOutOfRangeException: If the "start" index is outside the range of valid indexes for array or "count" is less than zero, or "start" index and count do not specify a valid section in array.
Example 1:
csharp
// C# program to demonstrate the
// Array.LastIndexOf(Array,
// Object, Int32, Int32) method
using System;
class GFG {
// Main Method
static void Main()
{
int[] arr = {1, 5, 6, 2, 6, 8, 2};
// search for "6"
// search start from index 5 and
// searches 2 indexes backward from index 5
// at first index 4 is comes where the value is 6
// so returns index 4
int indx1 = Array.LastIndexOf(arr, 6, 5, 2);
Console.WriteLine("First Index of 6 search from"+
" index 5 found at " + indx1);
// search for "2"
// search start from index 4 and
// searches 2 indexes backward from index 4
// at first index 3 is comes where the value is 2
// so returns index 3
int indx2 = Array.LastIndexOf(arr, 2, 4, 2);
Console.WriteLine("First Index of 2 search from"+
" index 4 found at " + indx2);
}
}
Output:
First Index of 6 search from index 5 found at 4
First Index of 2 search from index 4 found at 3
Example 2:
csharp
// C# program to demonstrate the
// Array.LastIndexOf(Array, Object,
// Int32, Int32) method
using System;
class GFG {
// Main Method
public static void Main()
{
// creating a array of type Array
Array arr = Array.CreateInstance(typeof(String), 8);
// 0, 1, 2, 3, 4, 5, 6, 7 are the
// indexes of the elements
arr.SetValue("java", 0);
arr.SetValue("C#", 1);
arr.SetValue("C++", 2);
arr.SetValue("C", 3);
arr.SetValue("C++", 4);
arr.SetValue("C#", 5);
arr.SetValue("Python", 6);
arr.SetValue("Ruby", 7);
String s1 = "C#";
// search for "C#"
// search start from index 6 and
// searches 3 indexes backward from index 6
// at first index 5 is comes where the value is "C#"
// so returns index 5
int index1 = Array.LastIndexOf(arr, s1, 6, 3);
Console.WriteLine("First Index of C# search from"+
" index 6 found at " + index1);
String s2 = "C++";
// search for "C++"
// search start from index 5 and
// searches 3 indexes backward from index 5
// at first index 4 is comes where the value is "C++"
// so returns index 4
int index2 = Array.LastIndexOf(arr, s2, 5, 3);
Console.WriteLine("First Index of C++ search from"+
" index 5 found at " + index2);
}
}
Output:
First Index of C# search from index 6 found at 5
First Index of C++ search from index 5 found at 4
Reference:
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
Decorators in Python In Python, decorators are a powerful and flexible way to modify or extend the behavior of functions or methods, without changing their actual code. A decorator is essentially a function that takes another function as an argument and returns a new function with enhanced functionality. Decorators are
10 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
AVL Tree Data Structure An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. The absolute difference between the heights of the left subtree and the right subtree for any node is known as the balance factor of
4 min read