How To Delete Minimum And Maximum From Array?

In this tutorial, we will learn How To Delete the Minimum And Maximum From an Array? for which we have to first find out the maximum and minimum values of an array using max and min functions of library math and then just erase them as per over requirement.

Minimum And Maximum

Let’s look at the example and understand it in a simpler way.

Minimum And Maximum

As we know array is a data structure whose elements can be directly accessed with the help of an index number of the array and removing an element also we just need to enter the index number and store the element stored preceding o it.

The array has n elements from which we can access any of the elements by just providing the

So let’s look at the example.

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] + " ");
    }
}

Here we iterated the whole array one by one and find out the maximum number among them and then removed it and shift all the elements by one towards left using for loop.

Similarly, we followed the same mechanism to find and remove the minimum number from the array and perform deletion upon it.

 

To learn more about How To Delete the Minimum And Maximum From the Array at the Program remove the maximum and minimum elements from an array.

And visit Java Tutorials for solutions to java problems and for learning more about python visit How To Reverse A String In Python?

Leave a Comment

%d bloggers like this: