Skip to content

Add MaximumElement implementation with documentation#7318

Open
AbhiramSakha wants to merge 2 commits intoTheAlgorithms:masterfrom
AbhiramSakha:master
Open

Add MaximumElement implementation with documentation#7318
AbhiramSakha wants to merge 2 commits intoTheAlgorithms:masterfrom
AbhiramSakha:master

Conversation

@AbhiramSakha
Copy link

📌 Description

Added a new implementation to find the maximum element in an array.

✅ Changes Made

  • Implemented findMax method using iteration
  • Added proper JavaDoc comments
  • Ensured clean and readable code

🧪 Testing

  • Verified logic with sample inputs

📋 Checklist

  • I have read CONTRIBUTING.md
  • This is my own work (no plagiarism)
  • Code follows Java naming conventions
  • Code is properly formatted

@codecov-commenter
Copy link

codecov-commenter commented Mar 17, 2026

Codecov Report

❌ Patch coverage is 0% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.41%. Comparing base (7d57c57) to head (4f4aa68).

Files with missing lines Patch % Lines
.../java/com/thealgorithms/arrays/MaximumElement.java 0.00% 5 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7318      +/-   ##
============================================
- Coverage     79.43%   79.41%   -0.03%     
+ Complexity     7062     7061       -1     
============================================
  Files           788      789       +1     
  Lines         23124    23129       +5     
  Branches       4545     4547       +2     
============================================
- Hits          18368    18367       -1     
- Misses         4022     4027       +5     
- Partials        734      735       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Contributor

@singhc7 singhc7 left a comment

Choose a reason for hiding this comment

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

Hey! Thanks for taking the time to put this together.

While reviewing this, I noticed that we actually already have established implementations handling this exact logic in the repository (such as FindMax.javaand AbsoluteMax.java in the maths package).

To keep the codebase lean and avoid unnecessary redundancy, it might be best to withdraw this specific PR. If you are looking to contribute, I'd highly recommend checking out the open issues to see where help is needed most right now!

Also left some general suggestions for your code for future reference.

* @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;

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants