diff --git a/MooreVoting.java b/MooreVoting.java new file mode 100644 index 000000000000..f9c848e38cda --- /dev/null +++ b/MooreVoting.java @@ -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(); + } +}