hello every one

I'm making a code to calculate the mode (most frequent value or values in an array )

assuming we have only one mode ,The code I made is giving the lowest value if it is repeated even if the actual mode is the maximum value so i expand the code with adding more lines to do the same but with the array inverted

by using bubble with < instead of >

the second part of the code will give the maximum value in the array even if the actual mode is the lowest .

if the mode (most frequent occurring value ) is some where midway but not the maximum nor the minimum value the code is returning it correctly and any one of the tow parts will solve it

i combined both codes into one and can not find a way to put IF ELSE statement or something similar to return only one of the tow results (result & result1) and eliminate the the possible wrong return that may be one of them

2,2,5,5,5,5,5 will return the '2' using the first part:confused: and 5 using the second part

2,2,2,2,2,2,2,5,5,5 will return 5 as result1 using the second part:confused: and 2 using the second part

2,2,2,5,5,5,5,5,5,5,9,9,9 will return the 5 correctly for both parts

tempcount and tempcount1 are used to hold the highest frequency and both will be equal in both cases

there is something fishy happening at the ends of the array :confused:

one of the tow output ore both is definitely the mode

this is my code

the class and the methods for ascending and descending sorting :Code:`[COLOR="Blue"] public double Mode( double [] data1 )`

{

this.Arrange(data1);

int count=0;

int tempcount=0;

for(int i=1; i<data1.length; i++)

for(int j=0; j<data1.length-1; j++)

{

if(data1[j]!= data1[j+1])

{

count=0;

}

if(data1[j]==data1[j+1])

{

count++;

}

if(count>tempcount)

{

tempcount = count;

result = data1[j];

}

}

this.RevArrange(data1);

int count1=0;

int tempcount1=0;

for(int i=1; i<data1.length; i++)

for(int j=0; j<data1.length-1; j++)

{

if(data1[j]!= data1[j+1]){count1=0;

}

if(data1[j]==data1[j+1])

{

count1++;

}

if(count1>tempcount1)

{

tempcount1 = count1;

result1 = data1[j];

}

}

JOptionPane.showMessageDialog(null,"one of the following tow values is the mode \n\n" + result+" \n and \n"+ result1 +"\n\nthere are possibly more modes\n\n\n\n\n\n\n"+"TESTING parameters <not data> "+"\n tempcount \n"+ tempcount+"\n tempcount1\n "+ tempcount1+"\n count\n" +count +"\ncount1\n"+count1 );

return result1;//ore result1 if else is needed here

}

[/COLOR]

what am i doing wrong ???:confused:Code:`[COLOR="Blue"]import javax.swing.*;`

class Statistics {

private double data1[];

private double data2[];

private int bn1;

private double result;

private double result1;

public Statistics(double data[],int bn){ // first constructor

data1 = data ;

bn1 = bn ;

}

public Statistics(double data[]){ //second constructor

data1 = data ;

}

public void Arrange( double [] data1 )

{

double temp;

for(int i=1; i<data1.length; i++)

for(int j=0; j<data1.length-1; j++)

if(data1[j]>data1[j+1]){

temp=data1[j];

data1[j]=data1[j+1];

data1[j+1]=temp;

}

}

public void RevArrange( double [] data1 )

{

double temp;

for(int i=1; i<data1.length; i++)

for(int j=0; j<data1.length-1; j++)

if(data1[j]<data1[j+1]){

temp=data1[j];

data1[j]=data1[j+1];

data1[j+1]=temp;

}

}[/COLOR]