Skip to content

Commit 574138c

Browse files
Cleanup BoyerMoore (TheAlgorithms#4951)
* modify code to make use of java Optional class * revert changes * add java.util.Optional<Integer> * add java.util.Optional * refactors: make `findmajor` return `optional` * refactors: make method name findMajor and split it * refactors: change method name in tests * Apply suggestions from code review Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> * change back to int * fix: swap arguments * tests: add some test cases * refactor: add `isMajority` and avoid rounding * style: use `var` * style: swap arguments of `countOccurrences` --------- Co-authored-by: vil02 <vil02@o2.pl> Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com>
1 parent d086afc commit 574138c

File tree

2 files changed

+40
-15
lines changed

2 files changed

+40
-15
lines changed

src/main/java/com/thealgorithms/others/BoyerMoore.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,50 @@
55
https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm
66
*/
77
package com.thealgorithms.others;
8+
import java.util.Optional;
89

910
public final class BoyerMoore {
1011
private BoyerMoore() {
1112
}
1213

13-
public static int findmajor(final int[] a) {
14+
public static Optional<Integer> findMajor(final int[] a) {
15+
final var candidate = findCandidate(a);
16+
final var count = countOccurrences(candidate, a);
17+
if (isMajority(count, a.length)) {
18+
return Optional.of(candidate);
19+
}
20+
return Optional.empty();
21+
}
22+
23+
private static int findCandidate(final int[] a) {
1424
int count = 0;
15-
int cand = -1;
25+
int candidate = -1;
1626
for (final var k : a) {
1727
if (count == 0) {
18-
cand = k;
28+
candidate = k;
1929
count = 1;
2030
} else {
21-
if (k == cand) {
31+
if (k == candidate) {
2232
count++;
2333
} else {
2434
count--;
2535
}
2636
}
2737
}
28-
count = 0;
38+
return candidate;
39+
}
40+
41+
private static int countOccurrences(final int candidate, final int[] a) {
42+
int count = 0;
2943
for (final var j : a) {
30-
if (j == cand) {
44+
if (j == candidate) {
3145
count++;
3246
}
3347
}
34-
if (count > (a.length / 2)) {
35-
return cand;
36-
}
37-
return -1;
48+
return count;
49+
}
50+
51+
private static boolean isMajority(final int count, final int totalCount) {
52+
return 2 * count > totalCount;
3853
}
3954
}

src/test/java/com/thealgorithms/others/BoyerMooreTest.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,22 @@
1111
public class BoyerMooreTest {
1212

1313
@ParameterizedTest
14-
@MethodSource("inputStream")
15-
void numberTests(int expected, int[] input) {
16-
Assertions.assertEquals(expected, BoyerMoore.findmajor(input));
14+
@MethodSource("inputStreamWithExistingMajority")
15+
void checkWhenMajorityExists(int expected, int[] input) {
16+
Assertions.assertEquals(expected, BoyerMoore.findMajor(input).get());
1717
}
1818

19-
private static Stream<Arguments> inputStream() {
20-
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}));
19+
private static Stream<Arguments> inputStreamWithExistingMajority() {
20+
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(4, new int[] {1, 4, 2, 4, 4, 5, 4}), Arguments.of(-1, new int[] {-1}));
21+
}
22+
23+
@ParameterizedTest
24+
@MethodSource("inputStreamWithoutMajority")
25+
void checkWhenMajorityExists(int[] input) {
26+
Assertions.assertFalse(BoyerMoore.findMajor(input).isPresent());
27+
}
28+
29+
private static Stream<Arguments> inputStreamWithoutMajority() {
30+
return Stream.of(Arguments.of(new int[] {10, 10, 20, 20, 30, 30}), Arguments.of(new int[] {10, 20, 30, 40, 50}), Arguments.of(new int[] {1, 2}), Arguments.of(new int[] {}));
2131
}
2232
}

0 commit comments

Comments
 (0)