Find the Middle Element of an 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 find the element which is present in the middle of the array.

If the array contains odd number of elements, then you will get one element present at the exact middle of the array.

If the array contains even number of elements, then you will get two elements present at the middle of the array.

In this article, you will see how to get the middle element of an array by using Java programming language. Let’s explore.

To Show You Some Instances

Instance-1

Suppose the original array is {1, 6, 5, 2, 9, 3, 4, 6}

After performing the array operation to detect the elements which is middle element of an array, the result will be −

The middle elements are 2 and 9

Instance-2

Suppose the original array is {1, 6, 5, 2, 4, 7, 9, 4, 6}

After performing the array operation to detect the elements which is middle element of an array, the result will be −

The middle element is 4

Instance-3

Suppose the original array is {2, 6, 6, 2, 7, 3, 4, 8, 9}

After performing the array operation to detect the element which is middle element of an array, the result will be −

The middle element is 7

Algorithm

  • Step 1 − Declare and initialize an integer array.

  • Step 2 − Check the length of an array and assign it to an int variable “nums”.

  • Step 3 − If nums is even then print the middle two elements by applying the logic.

  • Step 4 − If nums is odd then print the middle element by applying the logic.

Multiple Approaches

We have provided the solution in different approaches.

  • By Using Static Initialization of Array Elements

  • 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 Elements

Example

In this approach, array elements will be initialized in the program. Then as per the algorithm find the middle element of an array in Java.

public class Main{

   //main method
   public static void main(String[] args){
   
      //Declare and initialize the array elements
      int[] nums = new int[]{1, 6, 5, 2, 4, 7, 9, 4, 6};
       
      //logic implementation for middle element
      
      //if even number of array elements are present
      if (nums.length %2 == 0){
         System.out.println("The middle elements are: ");
         
         // even-length array (two middle elements)
         int x = nums[(nums.length/2) - 1];
         System.out.println(x);
         int y = nums[nums.length/2];
         System.out.println(y);
      } 
      
      //if odd number of array elements are present
      else {
      
         // odd-length array (only one middle element)
         int z = nums[nums.length/2];
         System.out.println("The middle elements is: ");
         System.out.println(z);
      }
   }
}

Output

The middle elements is: 
4

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 find the middle element of an array in Java.

public class Main{

   //main method
   public static void main(String[] args){
   
      //Declare and initialize the array elements
      int[] nums = new int[]{1, 6, 5, 2, 4, 7, 9, 4, 6, 3};
      
      //call the user defined method
      midArray(nums);
   }
   
   //user defined method to find mid element
   public static void midArray(int []nums){
   
      //logic implementation for middle element
      
      //if even number of array elements are present
      if (nums.length %2 == 0){
         System.out.println("The middle elements are: ");
         
         // even-length array (two middle elements)
         int x = nums[(nums.length/2) - 1];
         System.out.println(x);
         int y = nums[nums.length/2];
         System.out.println(y);
      } 
      
      //if odd number of array elements are present
      else{
      
         // odd-length array (only one middle element)
         int z = nums[nums.length/2];
         System.out.println("The middle elements is: ");
         System.out.println(z);
      }
   }
}

Output

The middle elements are: 
4
7

In this article, we explored how to find the middle element of an array by using Java programming language.

Updated on: 05-Jan-2023

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements