Skip to content

Instantly share code, notes, and snippets.

@thmain
Last active May 25, 2018 02:28
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/e86dc98110bbb0991055b74d4688c873 to your computer and use it in GitHub Desktop.
Save thmain/e86dc98110bbb0991055b74d4688c873 to your computer and use it in GitHub Desktop.
import java.util.*;
public class MajorityElementSorting {
public static void find(int [] arrA){
if(arrA.length==0)
return;
boolean found = false;
Arrays.sort(arrA);
int count = 1;
int x = arrA[0];
for (int i = 1; i <arrA.length ; i++) {
if(x==arrA[i]){
count++;
if(count>arrA.length/2) {
System.out.println("(Sorting)Element appearing more than n/2 times: " + x);
found = true;
break;
}
}else{
x = arrA[i];
count = 1;
}
}
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