Skip to content

Instantly share code, notes, and snippets.

@KodeSeeker
Created August 7, 2014 00:41
Show Gist options
  • Select an option

  • Save KodeSeeker/e386c4d31e742357b932 to your computer and use it in GitHub Desktop.

Select an option

Save KodeSeeker/e386c4d31e742357b932 to your computer and use it in GitHub Desktop.
Find an element that occurs more than 50% of the time in an array.
/**
Use Moore's counting algorithm to find a candidate.
Then check if the candidate occurs more than 50% of the time.
**/
public int findCandidate(int[] in)
{
if(in==null)
return -1;
// keep a variable to track the majority index.
int maj_index=0;
int count =1;
for(int i=0;i<in.length;i++)
{
if(in[maj_index]==in[i])
{
count++;
}
else
{
count--;
}
//count dropped to zero! So new maj_index.
if (count==0)
{
maj_index=i;
count=1;
}
return in [maj_index];
}
}
public int checkCandidate(int [] arr)
{
int cand= findCandidate(arr);
int count=0;
for(int i=0;i<arr.length;i++)
{
if (arr[i]== cand)
{
count++;
}
}
if(count>=(arr.length/2))
return cand;
else
return -1;// no majority element :(
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment