findMin<T extends Comparable<T>> function

T findMin<T extends Comparable<T>>(
  1. List<T> list
)

📌 Finds the minimum value in a list of comparable elements.

Throws an Exception if the list is empty.

Time Complexity: O(n)

Implementation

T findMin<T extends Comparable<T>>(List<T> list) {
  if (list.isEmpty) {
    throw Exception("Cannot find minimum in an empty list.");
  }

  T min = list[0];
  for (int i = 1; i < list.length; i++) {
    if (list[i].compareTo(min) < 0) {
      min = list[i];
    }
  }

  return min;
}