Tuesday, June 13, 2017

Dominator

Tags

Following Code will find Dominator in 1D Array:

Code:
public class Dominator {

public static void main(String[] nt) {
int[] a={-3,-3,-3,-3,-3,-3,-2,-2,-2,-2,-2,-2,-1,0,0,2,2,2,2,2,3,3,3,3};
int[] b=new int[a.length];
int i;
for(int x=0;x<a.length;x++) {
i=0;
for(int y=0;y<a.length;y++) {
if(x!=y)
if(a[x]==a[y])
{     i++;   }
}
b[x]=i;
}
int max=b[0],min=20000;
for(int x=0;x<b.length;x++) {
if(b[x]!=0) {
if(max<b[x]) {
max=b[x];
}
if(min>b[x]) {
min=b[x];
}
}
}
int j=0;
int[] c=new int[a.length];
for(int x=0;x<b.length;x++) {
if(b[x]==max) {
c[j++]=a[x];
}
}
max=c[0];
for(int x=0;x<a.length;x++) {
if(c[x]!=0)
if(max<c[x]){
max=c[x];
}
}
System.out.println("Most  Repeated Max is :"+max);
c=new int[a.length];
j=0;
for(int x=0;x<a.length;x++) {
if(b[x]==min)
{
c[j++]=a[x];
}
}
min=c[0];
for(int x=0;x<a.length;x++) {
if(c[x]!=0)
if(min>c[x]){
min=c[x];
}
}
System.out.println("Least Repeated Min is :"+min);
}

}

Output: