Separate Odd and Even Elements into Two Separate Arrays 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 separate odd and even elements into two separate arrays and print the result.

A number is said to be an even number if it is divisible by 2, else the number is an odd number.

Note − The array must be an integer array.

In this article, you will see how to separate even and odd numbers of an array into two different arrays by using Java programming language.

Let’s start.

To Show You Some Instances

Instance-1

Suppose the original array is {21, 53, 99, 9, 67, 66, 2, 91}

After separating the two arrays, the result will be −

Even numbers are: [66, 2]
Odd numbers are: [21, 53, 99, 9, 67, 91]

Instance-2

Suppose the original array is {12, 23, 11, 64, 5, 87, 22, 67, 100}

After separating the two arrays, the result will be −

Even numbers are: [12, 64, 22, 100]
Odd numbers are: [23, 11, 5, 87, 67]

Instance-3

Suppose the original array is {11, 22, 33, 44, 55, 66, 77, 88, 99}

After separating the two arrays, the result will be −

Even numbers are: [22, 44, 66, 88]
Odd numbers are: [11, 33, 55, 77, 99]

Algorithm

  • Step 1 − Declare and initialize an integer array.

  • Step 2 − For even elements in an array, we check for “arr[i]%2==0”.

  • Step 3 − For even elements in an array, we check for “arr[i]%2==1”.

  • 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 and for Loop

  • By Using Static Initialization of Array and while Loop

  • 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 and for Loop

Example

In this approach, array elements will be initialized in the program. Then as per the algorithm separate odd and even elements into two separate arrays. Here, we have used for loop for traversing array.

public class Main{

   //main method
   public static void main(String[] args){
   
      //Declare and initialize the array elements
      int arr[] = { 21, 53, 99, 9, 67, 66, 2, 91 };
      
      //get the length of the array
      int size = arr.length;
      
      //Logic for even array elements
      System.out.println("Even numbers are:");
      for(int i=0; i<size; i++){
         if(arr[i]%2==0){
         
            //separates even numbers
            System.out.print(arr[i]+" ");
         }
      }
      
      //Logic for odd array elements
      System.out.println("\nOdd numbers are:");
      for(int i=0; i<size; i++){
         if(arr[i]%2==1){
         
            //separates odd numbers
            System.out.print(arr[i]+" ");
         }
      }
   }
}

Output

Even numbers are:
66 2 
Odd numbers are:
21 53 99 9 67 91

Approach-2: By Using Static Initialization of Array and while Loop

Example

In this approach, array elements will be initialized in the program. Then as per the algorithm separate odd and even elements into two separate arrays. Here, we have used while loop for traversing array.

public class Main {

   //main method
   public static void main(String[] args){
   
      //Declare and initialize the array elements
      int arr[] = { 21, 53, 99, 9, 67, 66, 2, 91 };
      
      //get the length of the array
      int size = arr.length;
      
      //Logic for even array elements
      System.out.println("Even numbers are:");
      int i=0;
      while(i<size){
         if(arr[i]%2==0){
         
            //separates even numbers
            System.out.print(arr[i]+" ");
         }
         i++;
      }
      
      //Logic for odd array elements
      System.out.println("\nOdd numbers are:");
      i=0;
      while(i<size){
         if(arr[i]%2==1){
         
            //separates odd numbers
            System.out.print(arr[i]+" ");
         }
         i++;
      }
   }
}

Output

Even numbers are:
66 2 
Odd numbers are:
21 53 99 9 67 91

Approach-3: By Using User Defined Method

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 separate odd and even elements into two separate arrays.

public class Main{

   //main method
   public static void main(String[] args){
   
      //Declare and initialize the array elements
      int arr[] = {12, 23, 11, 64, 5, 87, 22, 67, 100};
      
      // calling the method
      printArray(arr);
   }
   
   //user defined method to separate odd and even elements
   public static void printArray(int []arr){
   
      //get the length of the array
      int size = arr.length;
      
      //Logic for even array element
      System.out.println("Even numbers are:");
      for(int i=0; i<size; i++){
         if(arr[i]%2==0){
         
            //separates even numbers
            System.out.print(arr[i]+" ");
         }
      }
      
      //Logic for odd array elements
      System.out.println("\nOdd numbers are:");
      for(int i=0; i<size; i++){
         if(arr[i]%2==1){
         
            //separates odd numbers
            System.out.print(arr[i]+" ");
         }
      }
   }
}

Output

Even numbers are:
12 64 22 100 
Odd numbers are:
23 11 5 87 67

In this article, we explored how to separate odd and even elements of an array into two separate arrays by using Java programming language.

Updated on: 11-Jan-2023

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements