Skip to content

Absolute max branch #5180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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
15 changes: 5 additions & 10 deletions src/main/java/com/thealgorithms/maths/AbsoluteMax.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,11 @@ private AbsoluteMax() {
* @return The absolute maximum value.
* @throws IllegalArgumentException If the input array is empty or null.
*/
public static int getMaxValue(int... numbers) {
if (numbers == null || numbers.length == 0) {
throw new IllegalArgumentException("Numbers array cannot be empty or null");
public static Optional<Integer> getMaxValue(List<Integer> numbers) {
if (numbers.isEmpty()) {
throw new IllegalArgumentException("Numbers array cannot be empty or null");
}
int absMax = numbers[0];
for (int i = 1; i < numbers.length; i++) {
if (Math.abs(numbers[i]) > Math.abs(absMax)) {
absMax = numbers[i];
}
}
return absMax;
return numbers.stream()
.max(Comparator.naturalOrder());
}
}
Loading