0

In this program I want to be able to remove the minimum and maximum values from the array but I don't know how to do it.

public class Average
{

  public static void main (String [ ] args) 
  {

    double [] weights = {39.5, 34.8, 22.6, 38.7, 25.4, 30.1, 41.8, 33.6, 26.2, 27.3};
    double minimum = Integer.MAX_VALUE;
    double maximum = Integer.MIN_VALUE;

    for(int i = 0; i < weights.length; i++){
        if(minimum > weights [i])
            minimum = weights[i];
    }

    for(int i = 0; i < weights.length; i++){
        if(maximum < weights [i])
            maximum = weights[i];
    }
  } 
}
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
  • 2
    copy to a new new array? excluding the min and max elements? What happens if these elements appear twice or more? – Scary Wombat Jul 27 '18 at 01:48
  • 5
    You can not really remove or add to arrays. You are better off storing everything in a List. An ArrayList would work nicely here. Run the same loop you have above to find the maximum and minimum numbers, store them in variables, and just do ArrayList.remove(maximum); ArrayList.remove(minimum); – gkgkgkgk Jul 27 '18 at 01:51
  • whosoever is voting the question and/or the answer down, care to explain why you are doing so? I hope you know you lose reputation points for down votes.. – aman_novice Jul 27 '18 at 01:56

3 Answers3

2

With java 8, you can use a stream to filter out of your array what you don't want. In order to use your minimum and maximum variable inside of the filter method, you have to put them in a final variable. The w that you see in the filter method, is a local variable that represents each element value.

You also don't have to run through your array twice to find the minimum and the maximum. Just use if/else if to set the appropriate min/max.

import java.util.Arrays;

public class StackOverflow {
    public static void main(String args[]) {
        double[] weights = { 39.5, 34.8, 22.6, 38.7, 25.4, 30.1, 22.6, 41.8, 33.6, 26.2, 27.3 };
        double minimum = Double.MAX_VALUE;
        double maximum = Double.MIN_VALUE;

        for (int i = 0; i < weights.length; i++) {
            if (minimum > weights[i])
                minimum = weights[i];
            else if (maximum < weights[i])
                maximum = weights[i];
        }

        System.out.println("Before: " + Arrays.toString(weights));

        final double minimumFilter = minimum;
        final double maximumFilter = maximum;
        weights = Arrays.stream(weights)
                .filter(w -> w != minimumFilter && w != maximumFilter)
                .toArray();

        System.out.println("After : " + Arrays.toString(weights));
    }
}

Result:

Before: [39.5, 34.8, 22.6, 38.7, 25.4, 30.1, 22.6, 41.8, 33.6, 26.2, 27.3]
After : [39.5, 34.8, 38.7, 25.4, 30.1, 33.6, 26.2, 27.3]
// Two 22.6 minimum was removed and one 41.8 maximum was removed.
zlakad
  • 1,314
  • 1
  • 9
  • 16
Shar1er80
  • 9,001
  • 2
  • 20
  • 29
0

There are multiple approaches to this problem.

  1. Traverse the array and skip the indexes you want and store all the remaining values to new array.
  2. Use Array copy to copy the elements without min and max values.
  1. But I would suggest that use collection framework for this purpose.
List<Double> weights = new ArrayList<Double>();

Instead of normal array preferable will be use ArrayList so that removal of any node becomes easy by providing index only. but still if you want to use Array only you can convert the Array to List and then remove the elements and again convert List to Array.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class Average {

    public static void main(String[] args) {

        double[] weights = { 39.5, 34.8, 22.6, 38.7, 25.4, 30.1, 41.8, 33.6, 26.2, 27.3 };
        double minimum = Integer.MAX_VALUE;
        double maximum = Integer.MIN_VALUE;
        int minIndex = -1;
        int maxIndex = -1;

        for (int i = 0; i < weights.length; i++) {
            if (minimum > weights[i]) {
                minimum = weights[i];
                minIndex = i;
            }
        }

        for (int i = 0; i < weights.length; i++) {
            if (maximum < weights[i]) {
                maximum = weights[i];
                maxIndex = i;
            }
        }

        List<Double> list = Arrays.stream(weights).boxed().collect(Collectors.toList());
        list.remove(minIndex);
        if(minIndex<maxIndex)
            list.remove(maxIndex-1);
        else
            list.remove(maxIndex);

        weights = list.stream().mapToDouble(Double::doubleValue).toArray();

        for (double weight : weights) {
            System.out.print(weight + " ");
        }
    }
}
Ashu
  • 2,066
  • 3
  • 19
  • 33
0

I think you'd better encapsulate the logic behind in a tool method as follows:

// all minimums and maximums in the array will be deleted and return a brand-new array;
public static Double[] removeMinMax(Double[] arr) {
    if (arr == null || arr.length == 0) throw new IllegalArgumentException();
    // a trick to be used avoid lambda `effectively final` check;
    Double[] minMax = {arr[0], arr[0]};
    for (int i = 0; i < arr.length; ++i) {
        if (minMax[0].compareTo(arr[i]) > 0) minMax[0] = arr[i];
        if (minMax[1].compareTo(arr[i]) < 0) minMax[1] = arr[i];
    }
    return Arrays.stream(arr)
            .filter(a -> a.compareTo(minMax[0]) != 0 && a.compareTo(minMax[1]) != 0)
            .toArray(Double[]::new);
}

As your input and have a local test as:

public static void main(String... args) {
    Double[] weights = {39.5, 34.8, 22.6, 38.7, 25.4, 30.1, 41.8, 33.6, 26.2, 27.3};
    Double[] ret = removeMinMax(weights);
    System.out.println(Arrays.toString(ret));
}

The result will be:

// minimum 22.6 and maximum 41.8 are removed
[39.5, 34.8, 38.7, 25.4, 30.1, 33.6, 26.2, 27.3]
Hearen
  • 7,420
  • 4
  • 53
  • 63