Reputation: 331
The user enters x-amount of integers, which are stored in an array ('dataA'). The average of the data is calculated, and the element, which is furthest should be removed ('outlier'). I don't know how to remove the element and need to know.
Also, the program removes one outlier, it calculates the new average and removes the next outlier until there is one element left.
public static Scanner sc1 = new Scanner(System.in);
public static int dataN = sc1.nextInt();//number of data elements
public static double[] dataA = new double[dataN];//array storing elements
for(int index = 0; index<dataA.length; index++)//increments index
{
double lengthA = dataA.length;//length of array
double avg = sum/lengthA;//avg of elements
double outlier = dataA[0];//outlier
double index_outlier = 0;//index of outlier
double dif_in = Math.abs(avg - dataA[index]);//difference of avg & element
double dif_out = Math.abs(avg - outlier);//difference of avg & outlier
if(dif_in > dif_out)
{
outlier = dataA[index];
index_outlier = index;
}
Upvotes: 0
Views: 206
Reputation: 24167
You can try to swap the outlier with last element of Array and continue with array but consider one less element. In case you are fine with it then you can use Array
like:
public static void main(String[] args) throws FileNotFoundException {
double[] dataArray = new double[] {1.5,2.5,3.5,4.5,7.5,8.5,2.5};
int arraySizeToConsider = dataArray.length;
double outlier;
int index_outlier;
double avg;
double diffInOutlierAndAvg;
while(arraySizeToConsider > 0) {
outlier = dataArray[0];
index_outlier = 0;
avg = computeSum(dataArray,arraySizeToConsider) / (arraySizeToConsider);//avg of elements
diffInOutlierAndAvg = Math.abs(avg - outlier);
// find outlier
for(int index = 0; index<arraySizeToConsider; index++)//increments index
{
if(Math.abs(avg - dataArray[index]) > diffInOutlierAndAvg) {
outlier = dataArray[index];
index_outlier = index;
}
}
double temp = dataArray[arraySizeToConsider -1];
dataArray[arraySizeToConsider -1] = outlier;
dataArray[index_outlier] = temp;
arraySizeToConsider = arraySizeToConsider -1;
System.out.println("Average: " + avg + " Outlier: " + outlier + " index " + index_outlier + " array size to consider: " +arraySizeToConsider);
}
}
private static double computeSum(double[] array, int arraySizeToConsider) {
double sum = 0;
for (int i = 0; i < arraySizeToConsider; i++) {
sum = sum + array[i];
}
return sum;
}
And here is the output:
Average: 4.357142857142857 Outlier: 8.5 index 5 array size to consider: 6
Average: 3.6666666666666665 Outlier: 7.5 index 4 array size to consider: 5
Average: 2.9 Outlier: 4.5 index 3 array size to consider: 4
Average: 2.5 Outlier: 1.5 index 0 array size to consider: 3
Average: 2.8333333333333335 Outlier: 3.5 index 2 array size to consider: 2
Average: 2.5 Outlier: 2.5 index 0 array size to consider: 1
Average: 2.5 Outlier: 2.5 index 0 array size to consider: 0
There are some more optimizations that I have left to you to figure out.
Hint: Do we need to compute sum every time we find an outlier ;)?
Upvotes: 1
Reputation: 37
Since you need a variable size data structure use a LinkedList instead of primitive array.
Also a better solution to do this will be as follows: 1) Store all the values in a LinkedList. Sort it while add elements to the list using insertion sort or sort it after adding all elements using Collections.sort(list). 2) Find the average. This can be done while adding the elements in the list. No need to iterate over the list again. 3) Since all the values are in sorted manner, the outlier will be either the first or the last element in the LinkedList. Compare the diff_first and diff_last and remove the greater. 4) Recompute the average. For this you can use a very simple equation: new_avg = ((avg*l)-outlier_value)/(l-1); where l = number of values for which avg was calculated. 5) Repeat step 3 and 4 until only 1 element is left in the LinkedList.
Upvotes: 1
Reputation: 93
Best solution would be to create a new array that copies the elements from the previous one except for the one you want to delete (with a for loop and an array with 1 less space than yours) and then set your original array as the new one.
Upvotes: 1