Skip to content

Commit 35f23d2

Browse files
authored
refactor: BoyerMoore (#5395)
1 parent 5f6510f commit 35f23d2

File tree

2 files changed

+53
-26
lines changed

2 files changed

+53
-26
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,81 @@
1-
/* this Code is the illustration of Boyer moore's voting algorithm to
2-
find the majority element is an array that appears more than n/2 times in an array
3-
where "n" is the length of the array.
4-
For more information on the algorithm refer
5-
https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm
6-
*/
71
package com.thealgorithms.others;
82
import java.util.Optional;
93

4+
/**
5+
* Utility class implementing Boyer-Moore's Voting Algorithm to find the majority element
6+
* in an array. The majority element is defined as the element that appears more than n/2 times
7+
* in the array, where n is the length of the array.
8+
*
9+
* For more information on the algorithm, refer to:
10+
* https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm
11+
*/
1012
public final class BoyerMoore {
1113
private BoyerMoore() {
1214
}
1315

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)) {
16+
/**
17+
* Finds the majority element in the given array if it exists.
18+
*
19+
* @param array the input array
20+
* @return an Optional containing the majority element if it exists, otherwise an empty Optional
21+
*/
22+
public static Optional<Integer> findMajorityElement(int[] array) {
23+
if (array == null || array.length == 0) {
24+
return Optional.empty();
25+
}
26+
27+
int candidate = findCandidate(array);
28+
int count = countOccurrences(candidate, array);
29+
30+
if (isMajority(count, array.length)) {
1831
return Optional.of(candidate);
1932
}
2033
return Optional.empty();
2134
}
2235

23-
private static int findCandidate(final int[] a) {
36+
/**
37+
* Identifies the potential majority candidate using Boyer-Moore's Voting Algorithm.
38+
*
39+
* @param array the input array
40+
* @return the candidate for the majority element
41+
*/
42+
private static int findCandidate(final int[] array) {
2443
int count = 0;
2544
int candidate = -1;
26-
for (final var k : a) {
45+
for (int value : array) {
2746
if (count == 0) {
28-
candidate = k;
29-
count = 1;
30-
} else {
31-
if (k == candidate) {
32-
count++;
33-
} else {
34-
count--;
35-
}
47+
candidate = value;
3648
}
49+
count += (value == candidate) ? 1 : -1;
3750
}
3851
return candidate;
3952
}
4053

41-
private static int countOccurrences(final int candidate, final int[] a) {
54+
/**
55+
* Counts the occurrences of the candidate element in the array.
56+
*
57+
* @param candidate the candidate element
58+
* @param array the input array
59+
* @return the number of times the candidate appears in the array
60+
*/
61+
private static int countOccurrences(final int candidate, final int[] array) {
4262
int count = 0;
43-
for (final var j : a) {
44-
if (j == candidate) {
63+
for (int value : array) {
64+
if (value == candidate) {
4565
count++;
4666
}
4767
}
4868
return count;
4969
}
5070

51-
private static boolean isMajority(final int count, final int totalCount) {
71+
/**
72+
* Determines if the count of the candidate element is more than n/2, where n is the length of the array.
73+
*
74+
* @param count the number of occurrences of the candidate
75+
* @param totalCount the total number of elements in the array
76+
* @return true if the candidate is the majority element, false otherwise
77+
*/
78+
private static boolean isMajority(int count, int totalCount) {
5279
return 2 * count > totalCount;
5380
}
5481
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class BoyerMooreTest {
1111
@ParameterizedTest
1212
@MethodSource("inputStreamWithExistingMajority")
1313
void checkWhenMajorityExists(int expected, int[] input) {
14-
Assertions.assertEquals(expected, BoyerMoore.findMajor(input).get());
14+
Assertions.assertEquals(expected, BoyerMoore.findMajorityElement(input).get());
1515
}
1616

1717
private static Stream<Arguments> inputStreamWithExistingMajority() {
@@ -21,7 +21,7 @@ private static Stream<Arguments> inputStreamWithExistingMajority() {
2121
@ParameterizedTest
2222
@MethodSource("inputStreamWithoutMajority")
2323
void checkWhenMajorityExists(int[] input) {
24-
Assertions.assertFalse(BoyerMoore.findMajor(input).isPresent());
24+
Assertions.assertFalse(BoyerMoore.findMajorityElement(input).isPresent());
2525
}
2626

2727
private static Stream<Arguments> inputStreamWithoutMajority() {

0 commit comments

Comments
 (0)