I was thinking about the performance of calling List<T>.Indexof(item)
. I am not sure if it will be a O(n) performance for a sequential algorithm or O(log(n)) performance for a binary tree??
FindIndex(Predicate<T>) Method Syntax: public int FindIndex (Predicate<T> match); Parameter: match: It is the Predicate<T> delegate that defines the conditions of the element to search for. Return Value: It returns the index of the first occurrence of an element that matches the conditions defined by match, if found.
The IndexOf method returns the first index of an item if found in the List. C# List<T> class provides methods and properties to create a list of objects (classes). The IndexOf method returns the first index of an item if found in the List.
In C#, IndexOf() method is a string method. This method is used to find the zero-based index of the first occurrence of a specified character or string within the current instance of the string. The method returns -1 if the character or string is not found.
Using Reflector for .NET we can see:
public int IndexOf(T item) { return Array.IndexOf<T>(this._items, item, 0, this._size); } public static int IndexOf<T>(T[] array, T value, int startIndex, int count) { return EqualityComparer<T>.Default.IndexOf(array, value, startIndex, count); } internal virtual int IndexOf(T[] array, T value, int startIndex, int count) { int num = startIndex + count; for (int i = startIndex; i < num; i++) { if (this.Equals(array[i], value)) return i; } return -1; }
It's O(n)
according to MSDN.
This method performs a linear search; therefore, this method is an O(n) operation, where n is Count.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With