Find Maximum Value of an Array in Java
What you’ll learn
- How linear scan keeps a running maximum.
- How to write a reusable
findMaxmethod in Java. - Why this works for negatives and duplicates too.
Prerequisites
Java arrays, loops, conditions, and console output.
- You can iterate an array with
forand access values by index. - You understand that maximum means the greatest numeric value in the list.
The idea
Keep one current winner (max). Scan left to right and replace it whenever you see a bigger value. One pass is enough.
Live preview
Algorithm
Initialize
Set max to the first element.
Scan
Loop from index 1 to the end.
Update
If current value is larger, replace max.
Return
Return final max.
📜 Pseudocode
Pseudocode
function findMax(arr):
max = arr[0]
for i from 1 to arr.length - 1:
if arr[i] > max:
max = arr[i]
return max1
Find maximum (reference program)
java
public class Main {
static int findMax(int[] arr) {
int maxVal = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > maxVal) {
maxVal = arr[i];
}
}
return maxVal;
}
public static void main(String[] args) {
int[] array = {14, 7, 25, 31, 10, 42};
int maxValue = findMax(array);
System.out.println("Maximum value in the array: " + maxValue);
}
}📤 Output
Maximum value in the array: 42
2
All elements are negative
java
public class Main {
static int findMax(int[] arr) {
int maxVal = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > maxVal) {
maxVal = arr[i];
}
}
return maxVal;
}
public static void main(String[] args) {
int[] negatives = {-9, -3, -1, -7};
System.out.println("Maximum (least negative): " + findMax(negatives));
}
}📤 Output
Maximum (least negative): -1
Optimization tips
Keep one pass only; sorting is unnecessary when you just need maximum.
Initialize from first element to avoid sentinel pitfalls.
❓ FAQ
It picks an initial candidate from actual data. After one pass, max stores the largest value seen.
There is no maximum in an empty array. Check arr.length first and handle that case.
Yes. The same comparison logic works, and the largest (least negative) value is returned.
The algorithm returns the maximum value itself; duplicates do not change the answer.
No for this task. Sorting is usually slower than a single O(n) scan when you only need max.
One pass over n elements: O(n) time and O(1) extra space.
Input/output examples
| Input array | Output |
|---|---|
[14, 7, 25, 31, 10, 42] | 42 |
[-9, -3, -1, -7] | -1 |
Edge cases
Empty
No first element
Define behavior for empty arrays: throw, sentinel, or OptionalInt.
Single
One element
That element is the maximum.
Duplicate
Same max repeated
Result remains the same maximum value.
⏱️ Time and space complexity
| Task | Time | Extra space |
|---|---|---|
Find maximum in array of size n | O(n) | O(1) |
Summary
- Track one running maximum while scanning once.
- The approach naturally handles negatives and duplicates.
- Complexity is
O(n)time andO(1)extra space.
Did you know?
Finding the maximum in an unsorted array needs at least n − 1 comparisons in the worst case. A single left-to-right scan achieves O(n) time and O(1) extra space.
8 people found this page helpful
