Repository: TheAlgorithms/Java
Pull request #4728 (2023-10-08 14:20:47)

Fix:/Number of count of major element in Boyer Moore algorithm
<!--
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
-->

<!-- For completed items, change [ ] to [x] -->

- [ ] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [ ] This pull request is all my own work -- I have not plagiarized it.
- [ ] All filenames are in PascalCase.
- [ ] All functions and variable names follow Java naming conventions.
- [ ] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.

DESCRIPTION:

- Added a fix in BoyerMoore class present in others folder.
- Fix the variable value which counts the number of times a major element comes in an array.

Type Of Change:
- fix a bug in BoyerMoore class in main\java\com\thealogrithms\others 

satvik644: Fixes #4727
vil02: @satvik644 thanks for the contribution. I have allowed myself to:
- add some tests: 012f2dc2326ccdab664d1e04d6fbcc3481620b21, 3540bce2e90b10fdac4ce0509713d22af24a6e76,
- remove the `main` method and make some _basic linting_: 3081850cda2d56f86368a61e3e6b7503b185cb71

I hope you do not mind.

Closes #4727.

Patch:
diff --git a/src/main/java/com/thealgorithms/others/BoyerMoore.java b/src/main/java/com/thealgorithms/others/BoyerMoore.java
index 09235b52..d9d5b5d0 100644
--- a/src/main/java/com/thealgorithms/others/BoyerMoore.java
+++ b/src/main/java/com/thealgorithms/others/BoyerMoore.java
@@ -6,27 +6,28 @@ https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm
  */
 package com.thealgorithms.others;
 
-import java.util.*;
-
-public class BoyerMoore {
+public final class BoyerMoore {
+    private BoyerMoore() {
+    }
 
-    public static int findmajor(int[] a) {
+    public static int findmajor(final int[] a) {
         int count = 0;
         int cand = -1;
-        for (int i = 0; i < a.length; i++) {
+        for (final var k : a) {
             if (count == 0) {
-                cand = a[i];
+                cand = k;
                 count = 1;
             } else {
-                if (a[i] == cand) {
+                if (k == cand) {
                     count++;
                 } else {
                     count--;
                 }
             }
         }
-        for (int i = 0; i < a.length; i++) {
-            if (a[i] == cand) {
+        count = 0;
+        for (final var j : a) {
+            if (j == cand) {
                 count++;
             }
         }
@@ -35,15 +36,4 @@ public class BoyerMoore {
         }
         return -1;
     }
-
-    public static void main(String[] args) {
-        Scanner input = new Scanner(System.in);
-        int n = input.nextInt();
-        int[] a = new int[n];
-        for (int i = 0; i < n; i++) {
-            a[i] = input.nextInt();
-        }
-        System.out.println("the majority element is " + findmajor(a));
-        input.close();
-    }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/others/BoyerMooreTest.java b/src/test/java/com/thealgorithms/others/BoyerMooreTest.java
new file mode 100644
index 00000000..b614c140
--- /dev/null
+++ b/src/test/java/com/thealgorithms/others/BoyerMooreTest.java
@@ -0,0 +1,22 @@
+package com.thealgorithms.others;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import java.util.stream.Stream;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+public class BoyerMooreTest {
+
+    @ParameterizedTest
+    @MethodSource("inputStream")
+    void numberTests(int expected, int[] input) {
+        Assertions.assertEquals(expected, BoyerMoore.findmajor(input));
+    }
+
+    private static Stream<Arguments> inputStream() {
+        return Stream.of(Arguments.of(5, new int[] {5, 5, 5, 2}), Arguments.of(10, new int[] {10, 10, 20}), Arguments.of(10, new int[] {10, 20, 10}), Arguments.of(10, new int[] {20, 10, 10}), Arguments.of(-1, new int[] {10, 10, 20, 20, 30, 30}), Arguments.of(4, new int[] {1, 4, 2, 4, 4, 5, 4}));
+    }
+}