Find element in an array in C#
This post will discuss how to check the existence of a given element in an array in C#. The solution should return true if the array contains the specified value; otherwise, false.
1. Using Enumerable.Contains() method (System.Linq)
The Enumerable.Contains() method provides a simple, clean way to determine whether a sequence contains a specified element or not. The following example demonstrates the usage of the Contains() method:
|
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 |
using System; using System.Linq; public static class Extensions { public static bool find<T>(this T[] array, T target) { return array.Contains(target); } } public class Example { public static void Main() { int[] array = { 1, 2, 3, 4, 5 }; int target = 4; bool isExist = array.find(target); if (isExist) { Console.WriteLine("Element found in the array"); } else { Console.WriteLine("Element not found in the given array"); } } } /* Output: Element found in the array */ |
2. Using Array.Exists() method
The Array.Exists() method is the recommended solution is to check the existence of an element in an array. The following code example shows how to implement this.
|
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 bool find<T>(this T[] array, T target) { return Array.Exists(array, x => x.Equals(target)); } } public class Example { public static void Main() { int[] array = { 1, 2, 3, 4, 5 }; int target = 4; bool isExist = array.find(target); if (isExist) { Console.WriteLine("Element found in the array"); } else { Console.WriteLine("Element not found in the given array"); } } } /* Output: Element found in the array */ |
3. Using Array.IndexOf() method
Another good solution is to use the Array.IndexOf() method that returns the index of the first occurrence of the specified element in this array and -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 bool find<T>(this T[] array, T target) { return Array.IndexOf(array, target) != -1; } } public class Example { public static void Main() { int[] array = { 1, 2, 3, 4, 5 }; int target = 4; bool isExist = array.find(target); if (isExist) { Console.WriteLine("Element found in the array"); } else { Console.WriteLine("Element not found in the given array"); } } } /* Output: Element found in the array */ |
4. Using Array.FindIndex() method
Array.FindIndex() returns the index of the first element that satisfies the provided predicate. If no element satisfies the condition, FindIndex returns -1.
|
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 bool find<T>(this T[] array, T target) { return Array.FindIndex(array, x => x.Equals(target)) != -1; } } public class Example { public static void Main() { int[] array = { 1, 2, 3, 4, 5 }; int target = 4; bool isExist = array.find(target); if (isExist) { Console.WriteLine("Element found in the array"); } else { Console.WriteLine("Element not found in the given array"); } } } /* Output: Element found in the array */ |
5. Using HashSet
HashSet class provides the Contains method to determine whether the set object contains the specified element. The following example converts the given array to a set and calls its Contains method:
|
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 |
using System; using System.Collections.Generic; public static class Extensions { public static bool find<T>(this T[] array, T target) { return new HashSet<T>(array).Contains(target); } } public class Example { public static void Main() { int[] array = { 1, 2, 3, 4, 5 }; int target = 4; bool isExist = array.find(target); if (isExist) { Console.WriteLine("Element found in the array"); } else { Console.WriteLine("Element not found in the given array"); } } } /* Output: Element found in the array */ |
6. Using Enumerable.Where() method (System.Linq)
The System.Linq.Enumerable.Where() method filters a sequence of values based on a predicate. The following code example demonstrates how we can use Where() 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 40 |
using System; using System.Linq; public static class Extensions { public static bool find<T>(this T[] array, T target) { try { array.Where(i => i.Equals(target)).First(); // or // array.First(i => i.Equals(target)); return true; } catch (InvalidOperationException) { return false; } } } public class Example { public static void Main() { int[] array = { 1, 2, 3, 4, 5 }; int target = 4; bool isExist = array.find(target); if (isExist) { Console.WriteLine("Element found in the array"); } else { Console.WriteLine("Element not found in the given array"); } } } /* Output: Element found in the array */ |
We can avoid the try-catch block by setting the default value with the DefaultIfEmpty() method:
|
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 bool find(this int[] array, int target) { return (array.Where(i => i.Equals(target)) .DefaultIfEmpty(-1) .First()) != -1; } } public class Example { public static void Main() { int[] array = { 1, 2, 3, 4, 5 }; int target = 4; bool isExist = array.find(target); if (isExist) { Console.WriteLine("Element found in the array"); } else { Console.WriteLine("Element not found in the given array"); } } } /* Output: Element found in the array */ |
7. Using Array.FindAll() method
Array.FindAll() method returns an array containing all the elements that match the specified predicate. We can use it as following to check for the existence of an element 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 |
using System; public static class Extensions { public static bool find<T>(this T[] array, T target) { T[] results = Array.FindAll(array, x => x.Equals(target)); return results.Length > 0; } } public class Example { public static void Main() { int[] array = { 1, 2, 3, 4, 5 }; int target = 4; bool isExist = array.find(target); if (isExist) { Console.WriteLine("Element found in the array"); } else { Console.WriteLine("Element not found in the given array"); } } } /* Output: Element found in the array */ |
8. 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 bool find<T>(this T[] array, T target) { EqualityComparer<T> comparer = EqualityComparer<T>.Default; for (int i = 0; i < array.Length; i++) { if (comparer.Equals(array[i], target)) { return true; } } return false; } } public class Example { public static void Main() { int[] array = { 1, 2, 3, 4, 5 }; int target = 4; bool isExist = array.find(target); if (isExist) { Console.WriteLine("Element found in the array"); } else { Console.WriteLine("Element not found in the given array"); } } } /* Output: Element found in the array */ |
That’s all about finding 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 :)