Skip to content

Cleanup BoyerMoore #4951

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

Merged
merged 14 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
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
35 changes: 25 additions & 10 deletions src/main/java/com/thealgorithms/others/BoyerMoore.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,50 @@
https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm
*/
package com.thealgorithms.others;
import java.util.Optional;

public final class BoyerMoore {
private BoyerMoore() {
}

public static int findmajor(final int[] a) {
public static Optional<Integer> findMajor(final int[] a) {
final var candidate = findCandidate(a);
final var count = countOccurrences(candidate, a);
if (isMajority(count, a.length)) {
return Optional.of(candidate);
}
return Optional.empty();
}

private static int findCandidate(final int[] a) {
int count = 0;
int cand = -1;
int candidate = -1;
for (final var k : a) {
if (count == 0) {
cand = k;
candidate = k;
count = 1;
} else {
if (k == cand) {
if (k == candidate) {
count++;
} else {
count--;
}
}
}
count = 0;
return candidate;
}

private static int countOccurrences(final int candidate, final int[] a) {
int count = 0;
for (final var j : a) {
if (j == cand) {
if (j == candidate) {
count++;
}
}
if (count > (a.length / 2)) {
return cand;
}
return -1;
return count;
}

private static boolean isMajority(final int count, final int totalCount) {
return 2 * count > totalCount;
}
}
20 changes: 15 additions & 5 deletions src/test/java/com/thealgorithms/others/BoyerMooreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,22 @@
public class BoyerMooreTest {

@ParameterizedTest
@MethodSource("inputStream")
void numberTests(int expected, int[] input) {
Assertions.assertEquals(expected, BoyerMoore.findmajor(input));
@MethodSource("inputStreamWithExistingMajority")
void checkWhenMajorityExists(int expected, int[] input) {
Assertions.assertEquals(expected, BoyerMoore.findMajor(input).get());
}

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}));
private static Stream<Arguments> inputStreamWithExistingMajority() {
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}));
}

@ParameterizedTest
@MethodSource("inputStreamWithoutMajority")
void checkWhenMajorityExists(int[] input) {
Assertions.assertFalse(BoyerMoore.findMajor(input).isPresent());
}

private static Stream<Arguments> inputStreamWithoutMajority() {
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[] {}));
}
}