Find index of an element in a List in C#
This post will discuss how to find the index of an element in a list in C#.
The solution should either return the index of the first occurrence of the required element or -1 if it is not present in the list.
1. Using List<T>.IndexOf() method
The recommended solution is to use the List<T>.IndexOf() method, which returns the index of the first occurrence of the specified element in this list, or -1 if there is no such element.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
using System; using System.Collections.Generic; public class Example { public static void Main() { List<int> list = new List<int>() {3, 5, 2, 7, 6}; int item = 5; int index = list.IndexOf(item); if (index != -1) { Console.WriteLine(String.Format("Element {0} is found at index {1}", item, index)); } else { Console.WriteLine("Element not found in the given list."); } } } /* Output: Element 5 is found at index 1 */ |
2. Using List<T>.FindIndex() method
The recommended solution is to use the List<T>.FindIndex() method that returns the index of the first occurrence of the specified element that matches the conditions defined by a specified predicate. This method returns -1 if an item that matches the conditions is not found.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
using System; using System.Collections.Generic; public class Example { public static void Main() { List<int> list = new List<int>() {3, 5, 2, 7, 6}; int item = 5; int index = list.FindIndex(a => a == item); if (index != -1) { Console.WriteLine(String.Format("Element {0} is found at index {1}", item, index)); } else { Console.WriteLine("Element not found in the given list."); } } } /* Output: Element 5 is found at index 1 */ |
3. Using Enumerable.Select() method (System.Linq)
The following code example demonstrates how we can use Enumerable.Select to project over a sequence of values and use both value and each element’s index to find the index of the first occurrence of the specified element in this list.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
using System; using System.Linq; using System.Collections.Generic; public class Example { public static void Main() { List<int> list = new List<int>() {3, 5, 2, 7, 6}; int item = 5; try { int index = list.Select((elem, index) => new {elem, index}) .First(p => p.elem == item) .index; Console.WriteLine(String.Format("Element {0} is found at index {1}", item, index)); } catch (InvalidOperationException) { Console.WriteLine("Element not found in the given list."); } } } /* Output: Element 5 is found at index 1 */ |
We can avoid try-catch block by using FirstOrDefault() method instead of First():
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
using System; using System.Linq; using System.Collections.Generic; public class Example { public static void Main() { List<int> list = new List<int>() {3, 5, 2, 7, 6}; int item = 5; int index = list.Select((element, index) => new {element, index}) .FirstOrDefault(x => x.element.Equals(item)) ?. index ?? -1; if (index != -1) { Console.WriteLine(String.Format("Element {0} is found at index {1}", item, index)); } else { Console.WriteLine("Element not found in the given list."); } } } /* Output: Element 5 is found at index 1 */ |
4. Performing Linear Search
A naive solution is to perform a linear search on the given list to determine whether the target element is present in the list.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
using System; using System.Collections.Generic; public static class Extensions { public static int FindIndex<T>(this List<T> list, T item) { EqualityComparer<T> comparer = EqualityComparer<T>.Default; for (int i = 0; i < list.Count; i++) { if (comparer.Equals(list[i], item)) { return i; } } return -1; } } public class Example { public static void Main() { List<int> list = new List<int>() {3, 5, 2, 7, 6}; int item = 5; int index = list.FindIndex(item); if (index != -1) { Console.WriteLine(String.Format("Element {0} is found at index {1}", item, index)); } else { Console.WriteLine("Element not found in the given list."); } } } /* Output: Element 5 is found at index 1 */ |
That’s all about finding the index of an element in a List in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)