Skip to content
Closed
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
48 changes: 48 additions & 0 deletions MooreVoting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import java.util.Scanner;

public class MooreVoting {

public static int findMajorityElement(int[] arr) {
int candidate = -1;
int count = 0;

// Phase 1: Find a candidate for majority element
for (int num : arr) {
if (count == 0) {
candidate = num;
}
count += (num == candidate) ? 1 : -1;
}

// Phase 2: Verify the candidate
count = 0;
for (int num : arr) {
if (num == candidate) {
count++;
}
}

return (count > arr.length / 2) ? candidate : -1;
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of elements: ");
int n = sc.nextInt();
int[] arr = new int[n];

System.out.println("Enter the elements:");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}

int majorityElement = findMajorityElement(arr);
if (majorityElement != -1) {
System.out.println("The majority element is: " + majorityElement);
} else {
System.out.println("There is no majority element.");
}

sc.close();
}
}
Loading