Find index of an element in an array in C#
This post will discuss how to find the index of an element in an array 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 array.
1. Using Array.IndexOf() method
The recommended solution is to use the Array.IndexOf() method that returns the index of the first occurrence of the specified element in this array.
|
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 |
using System; public static class Extensions { public static int findIndex<T>(this T[] array, T item) { return Array.IndexOf(array, item); } } public class Example { public static void Main() { int[] array = { 1, 2, 3, 4, 5 }; int item = 4; int index = array.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 array."); } } } /* Output: Element 4 is found at index 3 */ |
2. Using Array.FindIndex() method
The Array.FindIndex() method returns the index of the first element that satisfies the provided predicate, 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 24 25 26 27 28 29 |
using System; public static class Extensions { public static int findIndex<T>(this T[] array, T item) { return Array.FindIndex(array, val => val.Equals(item)); } } public class Example { public static void Main() { int[] array = { 1, 2, 3, 4, 5 }; int item = 4; int index = array.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 array."); } } } /* Output: Element 4 is found at index 3 */ |
3. Using Enumerable.Select() method
The System.Linq.Enumerable.Select() method projects each element of a sequence into a new form. The following code example demonstrates how we can use Select() to project over a sequence of values, and use both value and each element’s index to find the first occurrence of the specified element in this array.
|
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.Linq; using System.Collections.Generic; public static class Extensions { public static int findIndex<T>(this T[] array, T item) { try { return array .Select((element, index) => new KeyValuePair<T, int>(element, index)) .First(x => x.Key.Equals(item)).Value; } catch (InvalidOperationException) { return -1; } } } public class Example { public static void Main() { int[] array = { 1, 2, 3, 4, 5 }; int item = 4; int index = array.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 array."); } } } /* Output: Element 4 is found at index 3 */ |
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 27 28 29 30 31 32 33 |
using System; using System.Linq; public static class Extensions { public static int findIndex<T>(this T[] array, T item) { return array .Select((element, index) => new { element, index }) .FirstOrDefault(x => x.element.Equals(item)) ?. index ?? -1; } } public class Example { public static void Main() { int[] array = { 1, 2, 3, 4, 5 }; int item = 4; int index = array.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 array."); } } } /* Output: Element 4 is found at index 3 */ |
4. Performing Linear Search
A naive solution is to perform a linear search on the given array to determine whether the target element is present in the array.
|
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 T[] array, T item) { EqualityComparer<T> comparer = EqualityComparer<T>.Default; for (int i = 0; i < array.Length; i++) { if (comparer.Equals(array[i], item)) { return i; } } return -1; } } public class Example { public static void Main() { int[] array = { 1, 2, 3, 4, 5 }; int item = 4; int index = array.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 array."); } } } /* Output: Element 4 is found at index 3 */ |
That’s all about finding the index of an element in an array 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 :)