Skip to content
Open
Changes from all commits
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
29 changes: 29 additions & 0 deletions src/main/java/com/thealgorithms/arrays/MaximumElement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.thealgorithms.arrays;

/**
* Finds the maximum element in an array.
*/
public final class MaximumElement {

private MaximumElement() {
// utility class
}

/**
* Returns the maximum element in the array.
*
* @param arr input array
* @return maximum value
*/
public static int max(int[] arr) {
Copy link
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
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) {
if (num > max) {
max = num;
}
}

return max;
}
}
Loading