Home > OS >  How to delete minimum and maximum from array?
How to delete minimum and maximum from array?

Time:10-23

I'm a newbie in java. I'm trying to find minimum maximum element in array and then delete the minimum and maximum. This is the code I wrote, it's only working for maximum not for minimum.

public class delminmax {

    public static void main(String[] args) {
        int [] nums = {10,50,20,90,22};
        int max = 0;
        int min = 0;
        for (int i=0; i<nums.length; i  ) {
            if (nums[i]> max)
                max = nums[i];
        }
        
        System.out.println("The max number is "  max);

        for (int i=0;i<nums.length;i  )
            if (max==nums[i]) {
                for (int j=i; j<nums.length-1;j  )
                    nums[j]= nums[j 1];
                
            }
        for (int i=0;i<nums.length-1;i  )
            System.out.print(nums[i]  " "   "\n");
        
        for (int i=0; i<nums.length; i  ) {
            if (nums[i]< min)
                min = nums[i];
        }
        System.out.println("The min number is "  min);
        
        
        for (int i=0;i<nums.length;i  )
            if (min==nums[i]) {
                for (int j=i; j<nums.length-1;j  )
                    nums[j]= nums[j 1];
                
            }
        for (int i=0;i<nums.length-1;i  )
            System.out.println(nums[i]   " ");
    }
}

CodePudding user response:

You can't delete elements from an array, an array has a fixed length determined at creation time. You can instead create a new array with two fewer elements. You only need one loop to determine the min and max, but instead of doing so directly it's cleaner in my opinion to track the indices of the min and max elements. Then you can use a second loop to skip those elements when you copy nums to the new array. Like,

int[] nums = { 10, 50, 20, 90, 22 };
int mini = 0, maxi = 0;
for (int i = 1; i < nums.length; i  ) {
    if (nums[i] < nums[mini]) {
        mini = i;
    }
    if (nums[i] > nums[maxi]) {
        maxi = i;
    }
}
System.out.printf("The min = %d, the max = %d%n", nums[mini], nums[maxi]);
int[] newNums = new int[nums.length - 2]; // A new array, with two fewer elements
int p = 0;
for (int i = 0; i < nums.length; i  ) {
    if (i != mini && i != maxi) {
        newNums[p  ] = nums[i];
    }
}
System.out.println(Arrays.toString(newNums));

Which outputs

The min = 10, the max = 90
[50, 20, 22]

CodePudding user response:

Using Java 8

You can try by using streams with the approach below:

Approach Here:

I have converted the given array of Integers in the sorted list and then find the minimum and maximum integer from that list (As it is a sorted list, the minimum integer will be at 0 index and maximum number will be at size-1 index) and then filtered the nums Array with these min and max integers and converted the filtered list back to Array of Integers.

One more way to find the minimum and maximum in array using IntSummaryStatistics:

IntSummaryStatistics statistics = Arrays.stream(nums).summaryStatistics();
        int minNum = statistics.getMin();
        int maxNum = statistics.getMax();

Note: It will remove all the occurrences of minimum and maximum integer from the given array.

    public class Test {
      public static void main(String[] args) {
        int [] nums = {10,50,90,20,5,90,22,5};

        List<Integer> mainList = Arrays.stream(nums).boxed().sorted()
                                       .collect(Collectors.toList());

        int minNum = mainList.get(0);
        int maxNum = mainList.get(mainList.size()-1);

        int[] num = Arrays.stream(nums)
                          .filter(x -> x != minNum && x != maxNum).toArray();

        for(int i:num){
            System.out.print(i   " ");
        }
    }
}

Testcases:

Input: {10,50,90,20,5,90,22,5}
Output: 10 50 20 22

Input: {10, 50, 20, 90, 22}
Output: 50 20 22
  • Related