0

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;      
        }
14
  • 1
    Arrays have a fixed size... that means you can't change the length of an array, all you can do is make a new array with a different size. Commented May 11, 2015 at 22:43
  • Right, so I don't need the size to change. Just to remove the element. Commented May 11, 2015 at 22:45
  • 1
    What are you going to replace the element with? Commented May 11, 2015 at 22:47
  • the element, which is furthest should be removed What exactly does this mean? Commented May 11, 2015 at 22:47
  • 2
    @ben1123 Well, "remove" implies the array is smaller after you remove something from it. (It contains everything it contained before, except for the thing you removed; therefore, it contains less things) Commented May 11, 2015 at 22:49

3 Answers 3

1

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 ;)?

Sign up to request clarification or add additional context in comments.

1 Comment

Hmmm... i'll have to think about that one
1

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.

2 Comments

How do I copy all the elements without the outlier? The number of elements inputted originally vary.
For (int i=0,i<(originalSize),i++){ Array2[i]==Array1[i]}.
1

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.

2 Comments

Why LinkedList rather than ArrayList? LinkedList is slower at 90% of the things people do.
I agree that LinkedList are slower than ArrayList but that is only when you are traversing through the list again and again. Whereas in this case the LinkedList is created only once. We need to look at the first and the last element of the list which is an O(1) operation since we always have reference to head and tail of the list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.