Java array remove – Java Program to Remove Even Numbers from Array

Java array remove: In the previous article, we have seen Java Program to Remove Odd Numbers from Array

In this article we are going to see how to remove even element from an array using java programming language. How to remove even integers from an array, Remove element from array java, Remove Array Element Java, Remove Value From Array Java, Delete Element From Array Java, Java Program To Print The Elements Of An Array Present On Even Position, Print Even Numbers In Array Java.

Java Program to Remove Even Numbers from Array

Array.remove java: An array is a data structure which stores a fixed-size sequential collection of values of a single type. Memory location is associated with each array element/value. Each array element has its own index where the array index starts from 0.

In an array, a set of variables is referenced by a single variable name and its array index position. It is also called a container object because it contains elements of similar types.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways to remove even numbers from an array.

Method-1: Java Program to Remove Even Numbers from Array By Static Initialization of Array Elements

Approach:

  • Count the number of odd elements using a for loop.
  • Initialize an array with size of the number of odd elements.
  • Iterate the original array and insert the odd elements into the new array.

Program:

import java.util.Scanner;

public class Main
{
    public static void main(String[] args) 
    {
        // initializing array
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
        System.out.print("Array before removing even numbers: ");
        printArray(arr);
        int[] result = removeEven(arr);
        // print the array
        System.out.print("\nArray after removing even numbers: ");
        printArray(result);
    }

    public static void printArray(int[] result) 
    {
        for (int i : result)
            System.out.print(i + " ");
    }

    public static int[] removeEven(int[] arr) 
    {
        // count number of odd numbers in the array
        int odd_count = 0;
        for (int i = 0; i < arr.length; i++) 
        {
            if (arr[i] % 2 != 0) 
            {
                odd_count++;
            }
        }
        // initialize new array with size of odd_count
        int[] result = new int[odd_count];
        int index = 0;
        // copy all odd numbers to the new array
        for (int i = 0; i < arr.length; i++) 
        {
            if (arr[i] % 2 != 0) 
            {
                result[index++] = arr[i];
            }
        }
        return result;
    }
}
Output:

Array before removing even numbers: 1 2 3 4 5 6 7 8 9 10 11 
Array after removing even numbers: 1 3 5 7 9 11

Method-2: Java Program to Remove Even Numbers from Array java By Dynamic Initialization of Array Elements

Approach:

  • Count the number of odd elements using a for loop.
  • Initialize an array with size of the number of odd elements.
  • Iterate the original array and insert the odd elements into the new array.

Program:

import java.util.Scanner;

public class Main
{
    public static void main(String[] args) 
    {
        // create scanner class object
        Scanner sc = new Scanner(System.in);
        
        // take input from user for array size
        System.out.println("Enter the size of array: ");
        int n = sc.nextInt();
        // initialize array with size n
        int[] arr = new int[n];
        
        // take input from user for array elements
        System.out.print("Enter array elements: ");
        for (int i = 0; i < n; i++) 
        {
            arr[i] = sc.nextInt();
        }
        
        System.out.println("Array before removing even numbers: ");
        printArray(arr);
        int[] result = removeEven(arr);
        // print the array
        System.out.println("\nArray after removing even numbers: ");
        printArray(result);
    }

    public static void printArray(int[] result) 
    {
        for (int i : result)
            System.out.print(i + " ");
    }

    public static int[] removeEven(int[] arr) 
    {
        // count number of odd numbers in the array
        int odd_count = 0;
        for (int i = 0; i < arr.length; i++) 
        {
            if (arr[i] % 2 != 0) 
            {
                odd_count++;
            }
        }
        // initialize new array with size of odd_count
        int[] result = new int[odd_count];
        int index = 0;
        // copy all odd numbers to the new array
        for (int i = 0; i < arr.length; i++) 
        {
            if (arr[i] % 2 != 0) {
                result[index++] = arr[i];
            }
        }
        return result;
    }
}
Output:

Enter the size of array: 
Enter array elements: Array before removing even numbers: 
1 2 3 4 5 6 7 8 9 
Array after removing even numbers: 
1 3 5 7 9

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Try these: 

  1. How To Remove Element From Array In Java?
  2. How To Remove Element From Array Java?
  3. How To Remove An Element From An Array Java?
  4. How To Remove An Element From An Array In Java?
  5. Write A Java Program To Remove A Specific Element From An Array?
  6. How To Remove Even Numbers From An Array In Java?

Related Java Programs: