mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-08 02:04:31 +08:00
Fix:/Number of count of major element in Boyer Moore algorithm (#4728)
* Number of count of major element in Boyer Moore algorithm * test: add `BoyerMooreTest` * style: basic linting * tests: add test case from the issue --------- Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> Co-authored-by: vil02 <vil02@o2.pl>
This commit is contained in:

committed by
GitHub

parent
9dde8a7808
commit
945e7b56bb
@ -6,27 +6,28 @@ https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm
|
||||
*/
|
||||
package com.thealgorithms.others;
|
||||
|
||||
import java.util.*;
|
||||
public final class BoyerMoore {
|
||||
private BoyerMoore() {
|
||||
}
|
||||
|
||||
public class BoyerMoore {
|
||||
|
||||
public static int findmajor(int[] a) {
|
||||
public static int findmajor(final int[] a) {
|
||||
int count = 0;
|
||||
int cand = -1;
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
for (final var k : a) {
|
||||
if (count == 0) {
|
||||
cand = a[i];
|
||||
cand = k;
|
||||
count = 1;
|
||||
} else {
|
||||
if (a[i] == cand) {
|
||||
if (k == cand) {
|
||||
count++;
|
||||
} else {
|
||||
count--;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
if (a[i] == cand) {
|
||||
count = 0;
|
||||
for (final var j : a) {
|
||||
if (j == cand) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
@ -35,15 +36,4 @@ public class BoyerMoore {
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner input = new Scanner(System.in);
|
||||
int n = input.nextInt();
|
||||
int[] a = new int[n];
|
||||
for (int i = 0; i < n; i++) {
|
||||
a[i] = input.nextInt();
|
||||
}
|
||||
System.out.println("the majority element is " + findmajor(a));
|
||||
input.close();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user