# How to Remove Even Numbers from Array in Java?

In Java, Array is an object. It is a non-primitive data type which stores values of similar data type.

As per the problem statement we have to delete or eliminate the even numbers present in the array and print the odd array.

Note − The array must be an integer array.

A number is said to be an even number if the number is divisible by 2.

In this article, you will see how to remove even numbers from an array by using Java programming language. Let’s explore.

### To Show You Some Instances

#### Instance-1

Suppose the original array is {2, 5, 16, 14, 31, 22, 19, 20}


After performing the operation, means after removing even numbers, the updated array is: [5, 31, 19]

#### Instance-2

Suppose the original array is {12, 15, 16, 14, 31, 22, 9, 21}


After performing the operation, means after removing even numbers, the updated array is: [15, 31, 9, 21]

#### Instance-3

Suppose the original array is {12, 15, 16, 17}


After performing the operation, means after removing even numbers, the updated array is: [15, 17]

### Algorithm

• Step 1 − Declare and initialize an integer array.

• Step 2 − Declare the for loop and check for odd elements.

• Step 3 − Store the odd elements in a new array.

• Step 4 − Print the elements of the array.

### Syntax

To get the length of an array (number of elements in that array), there is an inbuilt property of array i.e length

Below refers to the syntax of it −

array.length


Where, ‘array’ refers to the array reference.

### Multiple Approaches

We have provided the solution in different approaches.

• By Using Static Initialization of Array

• By Using User Defined Method

Let’s see the program along with its output one by one.

## Approach-1: By Using Static Initialization of Array

### Example

In this approach, array elements will be initialized in the program. Then as per the algorithm remove even numbers from array in Java.

import java.util.*;
public class Main{

//main method
public static void main(String[] args){

// variables
int countOdd = 0;
int odd[] = null;

//declared and initialized an array
int numbers[] = {12, 5 , 77, 14, 91, 21, 1, 50};

// count odd numbers
for (int i : numbers){
if (!(i % 2 == 0))
++countOdd;
}

// create array to store odd numbers
odd = new int[countOdd];

// check each element and insert
int i = 0;
for (int num : numbers) {
if (!(num % 2 == 0)) {
// odd
odd[i++] = num;
}
}

//print odd array
System.out.println("Array after removing even numbers are: ");
System.out.println(Arrays.toString(odd));
}
}


### Output

Array after removing even numbers are:
[5, 77, 91, 21, 1]


## Approach-2: By Using User Defined Method

### Example

In this approach, array elements will be initialized in the program. Then call a user defined method by passing the array as parameter and inside method as per the algorithm to remove even numbers from the array in Java.

import java.util.*;
public class Main{

//main method
public static void main(String[] args){

//declared and initialized an array
int numbers[] = { 44, 5 , 9, 15, 31, 22, 19, 48 };

//calling the user defined method
removeEven(numbers);
}

//user defined method to remove even numbers
public static void removeEven(int []numbers){

// variables
int countOdd = 0;
int odd[] = null;

// count odd numbers
for (int i : numbers){
if (!(i % 2 == 0))
++countOdd;
}

// create array to store ood numbers
odd = new int[countOdd];

// check each element and insert
int i = 0;
for (int num : numbers) {
if (!(num % 2 == 0)) {
// even
odd[i++] = num;
}
}

//print even array
System.out.println("Array after removing even numbers are: ");
System.out.println(Arrays.toString(odd));
}
}


### Output

Array after removing even numbers are:
[5, 9, 15, 31, 19]


In this article, we explored how to remove even numbers from an array by using Java programming language.