Bash Script to Find Largest Element in Array
Use a loop to compare each element and keep track of the largest value with
for element in "${array[@]}"; do if (( element > max )); then max=$element; fi; done.Examples
Inputarray=(3 5 1 9 2)
OutputLargest element is 9
Inputarray=(10 10 10)
OutputLargest element is 10
Inputarray=(-5 -2 -9 -1)
OutputLargest element is -1
How to Think About It
To find the largest element, start by assuming the first element is the largest. Then check each element one by one. If you find a bigger element, update your largest value. At the end, the stored value is the largest.
Algorithm
1
Initialize max with the first element of the array2
Loop through each element in the array3
Compare current element with max4
If current element is greater, update max5
After loop ends, max holds the largest element6
Print the largest elementCode
bash
array=(3 5 1 9 2) max=${array[0]} for element in "${array[@]}"; do if (( element > max )); then max=$element fi done printf "Largest element is %d\n" "$max"
Output
Largest element is 9
Dry Run
Let's trace the array (3 5 1 9 2) through the code
1
Initialize max
max = 3 (first element)
2
Compare 5 with max
5 > 3, so max = 5
3
Compare 1 with max
1 > 5? No, max stays 5
4
Compare 9 with max
9 > 5, so max = 9
5
Compare 2 with max
2 > 9? No, max stays 9
| Element | Max after comparison |
|---|---|
| 3 | 3 |
| 5 | 5 |
| 1 | 5 |
| 9 | 9 |
| 2 | 9 |
Why This Works
Step 1: Start with first element
We set max to the first element because we need a baseline to compare others.
Step 2: Compare each element
Using a for loop, we check if the current element is bigger than max.
Step 3: Update max if needed
If the current element is bigger, we update max to this new value.
Step 4: Result after loop
After checking all elements, max holds the largest value.
Alternative Approaches
Using sort command
bash
array=(3 5 1 9 2) sorted=($(printf "%s\n" "${array[@]}" | sort -nr)) max=${sorted[0]} printf "Largest element is %d\n" "$max"
This uses external sort command, which is easy but slower for large arrays.
Using while loop with index
bash
array=(3 5 1 9 2) max=${array[0]} i=1 while (( i < ${#array[@]} )); do if (( array[i] > max )); then max=${array[i]} fi ((i++)) done printf "Largest element is %d\n" "$max"
Uses index-based loop instead of for-each, useful if you need index.
Complexity: O(n) time, O(1) space
Time Complexity
The script checks each element once, so time grows linearly with array size.
Space Complexity
Only a few variables are used, so space stays constant regardless of array size.
Which Approach is Fastest?
The loop method is fastest and uses least memory; sorting is slower due to extra overhead.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Loop with comparison | O(n) | O(1) | Fastest, minimal memory |
| Sort and pick first | O(n log n) | O(n) | Simple but slower for large arrays |
| While loop with index | O(n) | O(1) | When index needed for extra logic |
Always initialize max with the first element to avoid errors with empty or negative values.
Beginners often forget to initialize max before the loop, causing wrong or empty results.