Created
August 27, 2017 19:25
-
-
Save thmain/e5fae88a86ef8ba5000a02af309e1be7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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