Skip to content

Instantly share code, notes, and snippets.

@thmain
Created August 27, 2017 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/e5fae88a86ef8ba5000a02af309e1be7 to your computer and use it in GitHub Desktop.
Save thmain/e5fae88a86ef8ba5000a02af309e1be7 to your computer and use it in GitHub Desktop.
public class MajorityElementBruteForce {
public static void find(int [] arrA){
boolean found = false;
for (int i = 0; i <arrA.length ; i++) {
int x = arrA[i];
int count = 1;
for (int j = i+1; j <arrA.length ; j++) {
if(x==arrA[j])
count++;
}
if(count>arrA.length/2) {
System.out.println("Element appearing more than n/2 times: " + x);
found = true;
}
}
if(!found)
System.out.println("No element appearing more than n/2 times");
}
public static void main(String[] args) {
int [] arrA = {1,3,5,5,5,5,4,1,5};
find(arrA);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment