Delete All Odd Elements from an Array in Java


An array is a collection of similar data types (such as integers, floats, or characters) that are stored together in contiguous memory locations and can be accessed using an index or a subscript.

Arrays are commonly used to store and manipulate large amounts of data in a program. They provide a convenient way to group related data together and allow for efficient access to individual elements of the array.

Here we have given an array which contains set of elements including even as well as odd elements and as per the problem statement we have to delete all odd elements from that array of integers.

Let’s start!

To show you some instances

Instance-1

Original array: [23, 12, 45, 65, 34, 23, 54, 76, 20]
Array after deleting odd elements: [12, 34, 54, 76, 20]

Instance-2

Original array: [-22, 45, 23, 65, -9, 26]
Array after deleting odd elements: [-22, 26]

Instance-3

Original array: [123, 345, 657, 342, 344]
Array after deleting odd elements: [342, 344]

Algorithm

Algorithm-1

  • Step-1 − Create a new array.

  • Step-2 − Iterate through the input array and adds even elements to a new array.

  • Step-3 − It then creates a new array with the even elements and returns it.

  • Step-4 − This program prints out the original array and the resulting array after deleting odd elements.

  • Step-5 − It uses a method to delete odd elements from an array of integers in Java.

Algorithm-2

  • Step-1 − Create an ArrayList with elements.

  • Step-2 − Iterates through the input array and add even elements to an ArrayList.

  • Step-3 − It then creates a new arraylist with the even elements and returns it.

  • Step-4 − This program prints out the original array and the resulting arraylist after deleting odd elements.

  • Step-5 − It uses an ArrayList to delete odd elements from an array of integers in Java.

Syntax

1. The length method in Java returns the length of the given array.

Below refers to the syntax of it −

inputArray.lenght

Where, ‘inputArray’ refers to the given array.

2. size() method returns the number of elements in a collection, such as an ArrayList or a LinkedList. It is a built-in method in Java and can be used to determine the size of a collection at any given time.

Below refers to the syntax of it −

evenList.size()

Where, ‘evenList’ refers to ArrayList to store the even elements from the input array.

3. The get() method returns the element at a specified index in a collection, such as an ArrayList or a LinkedList. It is a built-in method in Java and can be used to access a specific element in a collection by its index.

Below refers to the syntax of it −

evenList.size() 

Where, ‘evenList’ refers to ArrayList to store the even elements from the input array.

Multiple Approaches

We have provided the solution in different approaches.

  • By Using New Array

  • By Using ArrayList

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

Approach-1: By Using New Array

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-1 using a new array deletes all odd elements from that array.

Example

import java.util.Arrays;
public class Main {
   public static void main(String[] args) {
      
      // Initialize the array with some values
      int[] inputArray = {1, 2, 3, 4, 5, 6, 7, 8, 9};
      
      // Call the delOddElmts method to remove the odd elements from the array
      int[] resultArray = delOddElmts(inputArray);
      
      // Print the resulting array
      System.out.println("Original array: " + Arrays.toString(inputArray));
      System.out.println("Array after deleting odd elements: " + Arrays.toString(resultArray));
   }
   
   //User Defined Method that deletes all odd elements from an array of integers
   public static int[] delOddElmts(int[] arr) {
      
      // Create a new array to store the even elements from the input array
      int[] evenArr = new int[arr.length];
      int j = 0;
      
      // Iterate through the input array and add the even elements to the new array
      for (int i = 0; i < arr.length; i++) {
         if (arr[i] % 2 == 0) {
            evenArr[j] = arr[i];
            j++;
         }
      }
      
      // Create a new array with the exact size needed to store the even elements
      int[] result = new int[j];
      
      // Copy the even elements from the new array to the result array
      for (int i = 0; i < j; i++) {
         result[i] = evenArr[i];
      }
      
      // Return the result array
      return result;
   }
}

Output

Original array: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Array after deleting odd elements: [2, 4, 6, 8]

Approach-2: By Using ArrayList

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-2, by using the ArrayList delete all odd elements from that array.

Example

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Main {
   public static void main(String[] args) {
      
      // Initialize the array with some values
      int[] inputArray = {23,12,45,65,34,23,54,76,20};
      
      // Call the delOddElmts method to remove the odd elements from the array
      int[] resultArray = delOddElmts(inputArray);
      
      // Print the resulting array
      System.out.println("Original array: " + Arrays.toString(inputArray));
      System.out.println("Array after deleting odd elements: " + Arrays.toString(resultArray));
   }
   
   //User Defined Method that deletes all odd elements from an array of integers
   public static int[] delOddElmts(int[] arr) {
      
      // Create an ArrayList to store the even elements from the input array
      List<Integer> evenList = new ArrayList<Integer>();
      
      // Iterate through the input array and add the even elements to the ArrayList
      for (int i = 0; i < arr.length; i++) {
         if (arr[i] % 2 == 0) {
            evenList.add(arr[i]);
         }
      }
      
      // Create a new array with the same size as the ArrayList
      int[] result = new int[evenList.size()];
      
      // Copy the elements from the ArrayList to the result array
      for (int i = 0; i < evenList.size(); i++) {
         result[i] = evenList.get(i);
      }
      
      // Return the result array
      return result;
   }
}

Output

Original array: [23, 12, 45, 65, 34, 23, 54, 76, 20]
Array after deleting odd elements: [12, 34, 54, 76, 20]

In this article, we explored different approaches to delete all odd elements from an array by using Java programming language.

Updated on: 04-May-2023

190 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements