Find the maximum value in an array
Pythonarr = [3, 7, 2, 9, 5]
max_val = arr[0]
for x in arr:
if x > max_val:
max_val = x
print(max_val)Explanation
The code starts by assuming the first element is the maximum. Then it scans each value once. Whenever a larger value appears, max_val is updated. This is a classic linear scan over contiguous data.
Output
9
Real-life Example
A marks dashboard can scan one student's subject scores and find the highest score shown in the report card summary.