linearSearch function

int linearSearch(
  1. List<int> list,
  2. int target
)

๐Ÿ” Linear Search Algorithm

Searches for a target value inside a list using a simple linear traversal. Returns the index of the element if found, otherwise returns -1.

Implementation

int linearSearch(List<int> list, int target) {
  for (int i = 0; i < list.length; i++) {
    // ๐Ÿงช Checking each element
    if (list[i] == target) {
      return i;
    }
  }

  return -1; // โŒ Target not found
}