Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: align MaximumElement with project standards
  • Loading branch information
AbhiramSakha authored Mar 17, 2026
commit 4f4aa68f3a7eb7b48f502ed2a7c06138dd6c7a9b
16 changes: 10 additions & 6 deletions src/main/java/com/thealgorithms/arrays/MaximumElement.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package com.thealgorithms.arrays;

/**
* This class provides a method to find the maximum element in an array.
* Finds the maximum element in an array.
*/
public class MaximumElement {
public final class MaximumElement {

private MaximumElement() {
// utility class
}

/**
* Finds the maximum value in the given array.
* Returns the maximum element in the array.
*
* @param arr the input array
* @return the maximum element in the array
* @param arr input array
* @return maximum value
*/
public static int findMax(int[] arr) {
public static int max(int[] arr) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While it is always great to see new contributions, there are a few critical edge cases and efficiency improvements needed here for a robust utility class:

  • The method does not validate against null inputs. Passing a null array will throw an unhandled NullPointerException when the enhanced for-loop attempts to execute.
  • If an empty array (new int[0]) is passed, the loop is bypassed entirely and the method returns Integer.MIN_VALUE. Since an empty set technically has no maximum element, returning an arbitrary extreme integer can cause silent downstream bugs. This should throw an IllegalArgumentException instead.
  • Initializing max to Integer.MIN_VALUE relies on a magic constant. A cleaner, more efficient algorithmic standard is to validate the array length, set max = arr[0], and iterate starting from the second element.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While it is always great to see new contributions, there are a few critical edge cases and efficiency improvements needed here for a robust utility class:

  • The method does not validate against null inputs. Passing a null array will throw an unhandled NullPointerException when the enhanced for-loop attempts to execute.
  • If an empty array (new int[0]) is passed, the loop is bypassed entirely and the method returns Integer.MIN_VALUE. Since an empty set technically has no maximum element, returning an arbitrary extreme integer can cause silent downstream bugs. This should throw an IllegalArgumentException instead.
  • Initializing max to Integer.MIN_VALUE relies on a magic constant. A cleaner, more efficient algorithmic standard is to validate the array length, set max = arr[0], and iterate starting from the second element.

Update your code like this:

public static int max(int[] arr) {
if (arr == null) {
throw new IllegalArgumentException("Input array cannot be null");
}

if (arr.length == 0) {
    throw new IllegalArgumentException("Array must contain at least one element");
}

int max = arr[0];

for (int i = 1; i < arr.length; i++) {
    if (arr[i] > max) {
        max = arr[i];
    }
}

return max;

}

int max = Integer.MIN_VALUE;

for (int num : arr) {
Expand Down
Loading