From ad380dcaa4721e5bad5ba4517d380dec2b697ec9 Mon Sep 17 00:00:00 2001 From: Shraddha <42699578+shraddhavp@users.noreply.github.com> Date: Thu, 28 Oct 2021 00:33:51 +0530 Subject: [PATCH] Add Boyer moore voting algo (#2726) --- Others/BoyerMoore.java | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Others/BoyerMoore.java diff --git a/Others/BoyerMoore.java b/Others/BoyerMoore.java new file mode 100644 index 000000000..68f6aa6fd --- /dev/null +++ b/Others/BoyerMoore.java @@ -0,0 +1,40 @@ +/* this Code is the illustration of Boyer moore's voting algorithm to +find the majority element is an array that appears more than n/2 times in an array +where "n" is the length of the array. +For more information on the algorithm refer https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm + */ +package Others; +import java.util.*; + +public class BoyerMoore { + public static int findmajor(int [] a){ +int count=0; int cand=-1; +for(int i=0;i (a.length / 2)) + return cand; + 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