|
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 | | - */ |
7 | 1 | package com.thealgorithms.others; |
8 | 2 | import java.util.Optional; |
9 | 3 |
|
| 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 | + */ |
10 | 12 | public final class BoyerMoore { |
11 | 13 | private BoyerMoore() { |
12 | 14 | } |
13 | 15 |
|
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)) { |
18 | 31 | return Optional.of(candidate); |
19 | 32 | } |
20 | 33 | return Optional.empty(); |
21 | 34 | } |
22 | 35 |
|
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) { |
24 | 43 | int count = 0; |
25 | 44 | int candidate = -1; |
26 | | - for (final var k : a) { |
| 45 | + for (int value : array) { |
27 | 46 | 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; |
36 | 48 | } |
| 49 | + count += (value == candidate) ? 1 : -1; |
37 | 50 | } |
38 | 51 | return candidate; |
39 | 52 | } |
40 | 53 |
|
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) { |
42 | 62 | int count = 0; |
43 | | - for (final var j : a) { |
44 | | - if (j == candidate) { |
| 63 | + for (int value : array) { |
| 64 | + if (value == candidate) { |
45 | 65 | count++; |
46 | 66 | } |
47 | 67 | } |
48 | 68 | return count; |
49 | 69 | } |
50 | 70 |
|
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) { |
52 | 79 | return 2 * count > totalCount; |
53 | 80 | } |
54 | 81 | } |
0 commit comments