-
Notifications
You must be signed in to change notification settings - Fork 19.8k
/
Copy pathBoyerMoore.java
81 lines (74 loc) · 2.56 KB
/
BoyerMoore.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package com.thealgorithms.others;
import java.util.Optional;
/**
* Utility class implementing Boyer-Moore's Voting Algorithm to find the majority element
* in an array. The majority element is defined as the element that appears more than n/2 times
* in the array, where n is the length of the array.
*
* For more information on the algorithm, refer to:
* https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm
*/
public final class BoyerMoore {
private BoyerMoore() {
}
/**
* Finds the majority element in the given array if it exists.
*
* @param array the input array
* @return an Optional containing the majority element if it exists, otherwise an empty Optional
*/
public static Optional<Integer> findMajorityElement(int[] array) {
if (array == null || array.length == 0) {
return Optional.empty();
}
int candidate = findCandidate(array);
int count = countOccurrences(candidate, array);
if (isMajority(count, array.length)) {
return Optional.of(candidate);
}
return Optional.empty();
}
/**
* Identifies the potential majority candidate using Boyer-Moore's Voting Algorithm.
*
* @param array the input array
* @return the candidate for the majority element
*/
private static int findCandidate(final int[] array) {
int count = 0;
int candidate = -1;
for (int value : array) {
if (count == 0) {
candidate = value;
}
count += (value == candidate) ? 1 : -1;
}
return candidate;
}
/**
* Counts the occurrences of the candidate element in the array.
*
* @param candidate the candidate element
* @param array the input array
* @return the number of times the candidate appears in the array
*/
private static int countOccurrences(final int candidate, final int[] array) {
int count = 0;
for (int value : array) {
if (value == candidate) {
count++;
}
}
return count;
}
/**
* Determines if the count of the candidate element is more than n/2, where n is the length of the array.
*
* @param count the number of occurrences of the candidate
* @param totalCount the total number of elements in the array
* @return true if the candidate is the majority element, false otherwise
*/
private static boolean isMajority(int count, int totalCount) {
return 2 * count > totalCount;
}
}