linearSearch static method

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

Linear search: searches the list sequentially for the target element.

Returns the index of the target element if found, otherwise -1.

Implementation

static int linearSearch(List<String> list, String target) {
  for (int i = 0; i < list.length; i++) {
    if (list[i] == target) {
      return i; // Element found, return its index
    }
  }
  return -1; // Element not found, return -1
}