Find an element in a List in C#
This post will discuss how to find an element in the given list in C#. The solution should return true if the list 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.Collections.Generic; public static class Extensions { public static bool find<T>(this List<T> list, T target) { return list.Contains(target); } } public class Example { public static void Main() { List<int> list = new List<int> { 1, 2, 3, 4, 5 }; int target = 4; bool isExist = list.find(target); if (isExist) { Console.WriteLine("Element found in the list"); } else { Console.WriteLine("Element not found in the given list"); } } } /* Output: Element found in the list */ |
2. Using List.IndexOf() method
Another good solution is to use the List.IndexOf() method that returns the index of the first occurrence of the specified element in this list 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 30 |
using System; using System.Collections.Generic; public static class Extensions { public static bool find<T>(this List<T> list, T target) { return list.IndexOf(target) != -1; } } public class Example { public static void Main() { List<int> list = new List<int> { 1, 2, 3, 4, 5 }; int target = 4; bool isExist = list.find(target); if (isExist) { Console.WriteLine("Element found in the list"); } else { Console.WriteLine("Element not found in the given list"); } } } /* Output: Element found in the list */ |
3. Using List.FindIndex() method
The List.FindIndex() method 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 30 |
using System; using System.Collections.Generic; public static class Extensions { public static bool find<T>(this List<T> list, T target) { return list.FindIndex(x => x.Equals(target)) != -1; } } public class Example { public static void Main() { List<int> list = new List<int> { 1, 2, 3, 4, 5 }; int target = 4; bool isExist = list.find(target); if (isExist) { Console.WriteLine("Element found in the list"); } else { Console.WriteLine("Element not found in the given list"); } } } /* Output: Element found in the list */ |
4. Using List.FindAll() method
The List.FindAll() method returns a list containing all the elements that match the specified predicate. We can use it to check the existence of an element in the list, as shown below:
|
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 |
using System; using System.Collections.Generic; public static class Extensions { public static bool find<T>(this List<T> list, T target) { List<T> results = list.FindAll(x => x.Equals(target)); return results.Count > 0; } } public class Example { public static void Main() { List<int> list = new List<int> { 1, 2, 3, 4, 5 }; int target = 4; bool isExist = list.find(target); if (isExist) { Console.WriteLine("Element found in the list"); } else { Console.WriteLine("Element not found in the given list"); } } } /* Output: Element found in the list */ |
5. 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 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 40 41 42 43 44 45 |
using System; using System.Linq; using System.Collections.Generic; public static class Extensions { public static bool find<T>(this List<T> list, T target) { try { list.Where(i => i.Equals(target)).First(); // or // list.First(i => i.Equals(target)); // or // list.Single(i => i.Equals(target)); return true; } catch (InvalidOperationException) { return false; } } } public class Example { public static void Main() { List<int> list = new List<int> { 1, 2, 3, 4, 5 }; int target = 4; bool isExist = list.find(target); if (isExist) { Console.WriteLine("Element found in the list"); } else { Console.WriteLine("Element not found in the given list"); } } } /* Output: Element found in the list */ |
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 34 |
using System; using System.Linq; using System.Collections.Generic; public static class Extensions { public static bool find(this List<int> list, int target) { return (list.Where(i => i.Equals(target)) .DefaultIfEmpty(-1) .First()) != -1; } } public class Example { public static void Main() { List<int> list = new List<int> { 1, 2, 3, 4, 5 }; int target = 4; bool isExist = list.find(target); if (isExist) { Console.WriteLine("Element found in the list"); } else { Console.WriteLine("Element not found in the given list"); } } } /* Output: Element found in the list */ |
6. 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 bool find<T>(this List<T> list, T target) { EqualityComparer<T> comparer = EqualityComparer<T>.Default; foreach (T i in list) { if (comparer.Equals(i, target)) { return true; } } return false; } } public class Example { public static void Main() { List<int> list = new List<int> { 1, 2, 3, 4, 5 }; int target = 4; bool isExist = list.find(target); if (isExist) { Console.WriteLine("Element found in the list"); } else { Console.WriteLine("Element not found in the given list"); } } } /* Output: Element found in the list */ |
That’s all about finding 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 :)