How to Remove Odd 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 odd numbers present in the array and print the even array.

Note − The array must be an integer array.

A number is said to be an odd number if the array is not divisible by 2.

In this article, you will see how to remove odd 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, 7, 14, 31, 21, 19, 22}

After performing the operation, means after removing odd numbers, the updated array is: [2, 14, 22]

Instance-2

Suppose the original array is {44, 5, 9, 15, 31, 22, 12, 48}

After performing the operation, means after removing odd numbers, the updated array is: [44, 22, 12, 48]

Instance-3

Suppose the original array is {12, 5, 77, 14, 91, 21, 1, 50}

After performing the operation, means after removing odd numbers, the updated array is: [12, 14, 50]

Algorithm

  • Step 1 − Declare and initialize an integer array.

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

  • Step 3 − Store the even 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 odd numbers from array in Java. Here, we have used for loop for traversing the array.

import java.util.*;
public class Main{

   //main method
   public static void main(String[] args){
   
      // variables
      int countEven = 0;
      int even[] = null;
      
      //declared and initialized an array
      int numbers[] = {12, 5 , 77, 14, 91, 21, 1, 50};
      
      // count even numbers
      for (int i : numbers){
         if (i % 2 == 0)
         ++countEven;
      }

      // create array to store even numbers
      even = new int[countEven];
    
      // check each element and insert
      int i = 0;
      for (int num : numbers) {
         if (num % 2 == 0) { 
            // even
         even[i++] = num;
         }
      }
      
      //print even array
      System.out.println("Array after removing odd numbers are: ");
      System.out.println(Arrays.toString(even));
   }
}

Output

Array after removing odd numbers are:
[12, 14, 50]

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 odd 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
      removeOdd(numbers);    
   }
   
   //user defined method to remove odd numbers 
   public static void removeOdd(int []numbers){
   
      // variables
      int countEven = 0;
      int even[] = null;
      
      // count even numbers
      for (int i : numbers){
         if (i % 2 == 0)
         ++countEven;
      }
      
      // create array to store even numbers
      even = new int[countEven];
      
      // check each element and insert
      int i = 0;
      for (int num : numbers) {
         if (num % 2 == 0) {
         
            // even
            even[i++] = num;
         }
      }
      
      //print even array
      System.out.println("Array after removing odd numbers are: ");
       System.out.println(Arrays.toString(even));
   }
}

Output

Array after removing odd numbers are:
[44, 22, 48]

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

Updated on: 05-Jan-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements