Last active
May 25, 2018 02:28
-
-
Save thmain/e86dc98110bbb0991055b74d4688c873 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
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