0
0
Bash-scriptingHow-ToBeginner · 2 min read

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 array
2
Loop through each element in the array
3
Compare current element with max
4
If current element is greater, update max
5
After loop ends, max holds the largest element
6
Print the largest element
💻

Code

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

ElementMax after comparison
33
55
15
99
29
💡

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.

ApproachTimeSpaceBest For
Loop with comparisonO(n)O(1)Fastest, minimal memory
Sort and pick firstO(n log n)O(n)Simple but slower for large arrays
While loop with indexO(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.