Finding the max/min value in an array of primitives using Java
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Java provides a robust framework for handling arrays, and one of the fundamental operations is identifying the maximum and minimum values in an array of primitive types. This task can be efficiently accomplished using simple iteration or Java utility classes, depending on the specific requirements or constraints of the application.
Overview of Finding Max/Min Values
The problem of finding the maximum or minimum value within an array is a classic computing problem that can be solved with various algorithms. However, for primitive arrays in Java, simplicity and efficiency are often key considerations. Here, we explore basic methods with detailed explanations, including sample code snippets and best practices.
Using Iteration
Algorithm
The most straightforward and commonly used approach involves iterating through the array while keeping track of the current maximum or minimum value encountered.
Example Code: Finding Maximum
Example Code: Finding Minimum
Explanation
- Time Complexity: The time complexity for both methods is , where n is the number of elements in the array. Each element is visited only once.
- Space Complexity: The algorithm uses additional space, as it only stores local variables for current max/min values.
Utilizing Java Streams
Java 8 introduced the Streams API, which allows for a more functional approach to the problem. This approach can often make the code more concise and expressive.
Example Using Streams
Explanation
- Code Conciseness: The Streams API allows for more concise code by utilizing built-in functions like
max()andmin()on the stream of array elements. - Time Complexity: Similar to traditional iteration, this approach has a time complexity of .
- Functional Programming Paradigm: This method aligns well with the functional programming paradigm that emphasizes immutability and expression over procedural code.
Error Handling and Edge Cases
When working with arrays, it's crucial to handle potential edge cases and errors gracefully:
- Null or Empty Arrays: Always check if the input array is null or empty before attempting to find the max or min value. The examples above throw
IllegalArgumentExceptionin such cases. - Single Element Arrays: The algorithm will naturally handle arrays with a single element, returning that element as both the max and min.
- Arrays with Identical Elements: If all elements are the same, both algorithms will return the value of those elements, as it would be both the max and min.
Summary Table of Key Points
| Approach | Implementation | Time Complexity | Space Complexity | Description |
| Iterative | Loop through each element | Simple and imperative | ||
| Streams API | Use Arrays.stream() with max()/min() | Concise and functional | ||
| Error Handling | Check for null/empty arrays Handle exceptions appropriately | N/A | N/A | Robustness against invalid input |
Conclusion
Finding the maximum and minimum values in an array is a common task that can be handled with ease in Java. Whether through traditional iteration or utilizing the Streams API introduced in Java 8, each method has its place. The choice often depends on personal preference, coding standards within the project, or the need for readability versus performance. By handling edge cases and choosing the appropriate approach, developers can ensure robust and efficient implementation.
Related reading
- Finding the max/min value in an array of primitives using Java
- Finding the median of an unsorted array
- Finding the most tree-like hierarchy that explains the data
- Finding the n-degree neighborhood of a node
- Finding the second highest number in array in Java
- findResource returning null when module-info.java is present, why is that?
- Finding the position of the maximum element
- Finding the second smallest number from the given list using divide-and-conquer

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.