Repository: TheAlgorithms/Java
Pull request #6145 (2025-01-19 15:42:43)

Fix AbsoluteMin bug for equal absolute values
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

The AbsoluteMin class contains a bug in the getMinValue method. When two numbers have the same absolute value (e.g., 5 and -5), the method returns the number that appears later in the array. This behavior is inconsistent and may lead to unexpected results.

Explanation with Example.

Before Fix
5, -5 returns -5 
-5, 5 returns  5

After Fix
5, -5 returns  5 
-5, 5 returns  5

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

Thanks

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/6145?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 73.83%. Comparing base [(`5454e2f`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/5454e2ff626547eaac3edad1d1459f6bf5ca7e7f?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`e0869b1`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/e0869b19376604a0aa16f0944c3b91ac84dcfff4?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).
> Report is 1 commits behind head on master.

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #6145      +/-   ##
============================================
- Coverage     73.84%   73.83%   -0.02%     
- Complexity     5154     5156       +2     
============================================
  Files           661      661              
  Lines         17687    17687              
  Branches       3409     3409              
============================================
- Hits          13061    13059       -2     
- Misses         4119     4120       +1     
- Partials        507      508       +1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/6145?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

vil02: Similar to #6144.

Patch:
diff --git a/src/main/java/com/thealgorithms/maths/AbsoluteMin.java b/src/main/java/com/thealgorithms/maths/AbsoluteMin.java
index 1ffe6d2e..1b9575a3 100644
--- a/src/main/java/com/thealgorithms/maths/AbsoluteMin.java
+++ b/src/main/java/com/thealgorithms/maths/AbsoluteMin.java
@@ -19,7 +19,7 @@ public final class AbsoluteMin {
 
         var absMinWrapper = new Object() { int value = numbers[0]; };
 
-        Arrays.stream(numbers).skip(1).filter(number -> Math.abs(number) < Math.abs(absMinWrapper.value)).forEach(number -> absMinWrapper.value = number);
+        Arrays.stream(numbers).skip(1).filter(number -> Math.abs(number) <= Math.abs(absMinWrapper.value)).forEach(number -> absMinWrapper.value = Math.min(absMinWrapper.value, number));
 
         return absMinWrapper.value;
     }


Test patch:
diff --git a/src/test/java/com/thealgorithms/maths/AbsoluteMinTest.java b/src/test/java/com/thealgorithms/maths/AbsoluteMinTest.java
index 4b676ca6..dfca757f 100644
--- a/src/test/java/com/thealgorithms/maths/AbsoluteMinTest.java
+++ b/src/test/java/com/thealgorithms/maths/AbsoluteMinTest.java
@@ -15,7 +15,13 @@ public class AbsoluteMinTest {
 
     @Test
     void testGetMinValueWithNoArguments() {
-        Exception exception = assertThrows(IllegalArgumentException.class, () -> AbsoluteMin.getMinValue());
+        Exception exception = assertThrows(IllegalArgumentException.class, AbsoluteMin::getMinValue);
         assertEquals("Numbers array cannot be empty", exception.getMessage());
     }
+
+    @Test
+    void testGetMinValueWithSameAbsoluteValues() {
+        assertEquals(-5, AbsoluteMin.getMinValue(-5, 5));
+        assertEquals(-5, AbsoluteMin.getMinValue(5, -5));
+    }
 }