
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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.
- Related Articles
- How to Remove Odd Numbers from Array in Java?
- How to pull even numbers from an array in MongoDB?
- How to find the Odd and Even numbers in an Array in java?
- How to remove an element from an array in Java
- Finding even length numbers from an array in JavaScript
- Swift Program to find the EVEN numbers from the array
- How to remove a specific element from a JSON Array in Java?
- Print Even Positions Elements from an Array in Java
- Returning an array containing last n even numbers from input array in JavaScript
- Java program to print odd and even number from a Java array.
- How to remove object from array in MongoDB?
- Java Program to Remove Duplicates from an Array List
- C++ code to decrease even numbers in an array
- Java program to Print Odd and Even Number from an Array
- remove null value from a String array in Java
