That method isn't working
I am trying to calculate the mode I am using 2 methods
Code:
public static ArrayList duplicates(ArrayList modual)
{
Collections.sort(modual);
Double previous = (Double)modual.get(1);
ArrayList duplicate = new ArrayList();
for(int i = 0; i < modual.size()-1; i++)
{
double temp = (Double) (modual.get(i));
if(temp == previous)
{
duplicate.add(temp);
}
else
{
previous = (Double) modual.get(i);
}
}
for(int j = 0; j <= duplicate.size()-1; j++)
{
modual.remove(duplicate.get(j));
}
return modual;
}
the purpose of this code is to take in the arraylist and remove the duplicates
Code:
public static ArrayList mode(double array[]) throws IOException
{
double n;
int freq = 0;
int Mfreq = 0;
double Mn = array[0];
ArrayList modes = new ArrayList();
PrintStream fout = new PrintStream( new FileOutputStream("output.txt"));
for (int i = 0; i < array.length; i++)
{
n = array[i];
if(i == 0 || array[i] != array[i - 1])
{
for (int j = 0; j < array.length; j++)
{
if (array[j] == n)
{
freq++;
}
}
}
if(freq != 0)
{
modes.add(freq+" "+array[i]);
}
freq = 0;
if (freq > Mfreq)
{
Mn = n;
}
}
double mode = 0.0;
double oc = 0.0;
String str [] = new String [modes.size ()];
modes.toArray (str);
ArrayList modual = new ArrayList();
for(int i = 0; i <= modes.size() -1; i ++)
{
String temp = (String) (modes.get(i));
int pos = str[i].indexOf(' ') ;
String occ = str[i].substring(0 , pos);
int occurances = Integer.parseInt(occ);
occ = str[i].substring(pos, str[i].length());
double number = Double.parseDouble(occ);
if(occurances >= oc)
{
modual.add(number);
oc = occurances;
}
}
return modual;
}
the purpose of this method is to take in an array and caluclate the mode
Bookmarks