Join Nostr
2026-10-13 18:37:58 UTC

codementor on Nostr: Learn: Linear search What: check the items one by one until you find the target. ...

Learn: Linear search

What: check the items one by one until you find the target.
Cost: O(n) in the worst case, O(1) extra memory.
Use it when: the data is not sorted, is small, or you search only once. Sorting first costs O(n log n), more than one linear search.

```csharp
int[] a = { 7, 3, 9, 1 };
Console.WriteLine(Array.IndexOf(a, 9));
Console.WriteLine(Array.IndexOf(a, 5));
```

It prints 2, then -1 (not found). Array.IndexOf and List<T>.Contains are linear searches.

Read more: https://www.geeksforgeeks.org/dsa/linear-search/

#tip · TIP-033