Java Program to return the elements at the odd position in a list


What Is the Odd Position in A List?

In a Java environment, printing a return value at odd positions of the elements can perform by a control flow statement in an array list. A control flow statement checks the odd positions on the basis of step size in Java. A loop is a condition checking control flow method that helps to evaluate whether a particular condition is true or false. For this condition, the "odd positions" always take a place as first, third, fifth manner in a particular array list. By using the syntax, the build code will check this condition.

At step one a temporary value initializes with 0 to check directly in the list. Next sort the data index as they are even or odd. Where the string is the main path, it can be easier to find the return the elements of a list accordingly.

In this article we will learn and examine how to write a java code and implement the possible methods to get return the elements present at the odd position from a particular data list.

What is An Array and Its Elements?

Array is a similar types of data elements present in a particular list. In an array, a user can access an element by using index number. The search process is really easy and can be used efficiently.

  • Assume, here we have an array {2,12,23,7,6,15} and we need to find the odd position element present here in this particular array. Here we can see the result will be 2,23,6 are present in the list.

  • Array is collection of similar data sets of the same type elements. They are all continuous fixed-sized data.

  • The elements present here are denoted by N and after applying the iteration ends with N-1.

  • In java, there are so many methods to find an element's position from a particular array

    • For method

    • Stream API method

  • For is an old and classic method to iterate the array then print and write but, for stream API it is enough to use "Arrays. AsList() .stream().forEach(s.o::p)" to get a detailed result elements of those odd positions.

Algorithm to find the Elements at the Odd Position

Here is the general algorithm to find an element at the odd position in Java −

  • Step 1 − Start

  • Step 2 − Initialize the particular array present on odd

  • Step 3 − Repeat step print (Step 5)

  • Step 4 − Print array

  • Step 5 − Print

Example

public class OddPosition {  
   public static void main(String[] args) {     
      int [] arr = new int [] {11, 12, 13, 14, 15};  
      System.out.println("Odd position elements present in the array: ");  
      for (int i = 0; i < arr.length; i = i+2) {  
         System.out.println(arr[i]);  
      }  
   }  
}  

Output

Odd position elements present in the array: 
11
13
15

This is a simple and general example to find an odd position element using java. Now there are several approaches to get return the array elements present at the odd positions from a particular data list. Let’s dive into it.

Below approaches are followed to find out the elements at the odd position in a list −

  • Approach 1 − By finding an odd element by iteration with the value 1

  • Approach 2 − By position increment by 2 for receiving an odd position.

  • Approach 3 − By finding the odd element by flag pointer maintenance.

  • Approach 4: − By finding an odd element divisible by 2 or not.

By Finding an odd Element by Iteration with the Value 1

Process to get an odd element with iteration value 1: Iterator is an object file, which can be used to connect loops. To use an iterator in java there is a package called java.util.

  • Temp value initialization with 0.

  • Apply traverse.

  • Temp value check for each iteration and if the value is 0 then you will get a return or else just carry on with the process.

  • Increment of temp value by 1 after each process.

Example

import java.io.*;
import java.util.*;
public class TP {
   public static void main(String[] args){
      List<Integer> tp_list1 = new ArrayList<Integer>();
      tp_list1.add(100);
      tp_list1.add(200);
      tp_list1.add(300);
      tp_list1.add(400);
      tp_list1.add(500);
      tp_list1.add(600);
      int temp_val = 0;
      System.out.print("Elements present at odd position are : ");
      for (Integer numbers : tp_list1) {
         if (temp_val % 2 != 0) {
            System.out.print(numbers + " ");
         }
         temp_val += 1;
      }
   }
}

Output

Elements present at odd position are: 200 400 600

By Position Increment by 2 for Getting an odd Position

Traversing the array elements when the element position is odd then increase it by 1.

Steps to be followed in increment by 2 −

  • Traverse list from the first position.

  • Apply increment process by 2 for each operation.

  • End the process after the iteration is done.

  • First iteration - 1+2=3

  • Second iteration - 2+3=5

  • Third iteration - 5+2=7

  • Continue with the flow

  • Return

Example

import java.io.*;
import java.util.*;
public class TP {
   public static void main(String[] args){
      List<Integer> tp_list2 = new ArrayList<>();
      tp_list2.add(1000);
      tp_list2.add(2000);
      tp_list2.add(3000);
      tp_list2.add(4000);
      tp_list2.add(5000);
      tp_list2.add(6000);
      System.out.print(
      "Elements at odd positions in that array are : ");
      for (int i = 1; i < 6; i = i + 2) {
         System.out.print(tp_list2.get(i) + " ");
      }
   }
}

Output

Elements at odd positions in that array are: 2000 4000 6000

By Finding the odd Element by Flag Pointer Maintenance

In a sorted array contained with positive integers, the first element's value should be maximum and second one is minimum and so on. In this process the pointer will be initialized to 1 to start the iteration.

Steps to get an odd element with iteration value 1 are −

  • Start

  • Pointer initialized to 1

  • Start iteration

  • If the flag is 1, then print the data

  • Change flag to 0

  • Else, if the flag throws 0 then change it to 1

  • End

Example

import java.util.*;
public class PrintOddElementsInArray {
   public static void main(String[] args){
      int inputArray[] = new int[] { 1000, -5000, 4500, -2000,
                        10000, -2130, 7500 };
      System.out.println("Existing array elements.");
      for (int i = 0; i < inputArray.length; i++) {
         System.out.println(inputArray[i]);
      }
            
      System.out.println(
      "Array elements at odd position.");
      int flag = 1;
      for (int i = 0; i < inputArray.length; i++) {
         if (flag == 1) {
            System.out.print(inputArray[i] + " ");
            flag = 0;
         }
         else
         flag = 1;
      }
   }
}

Output

Existing array elements ..
1000
-5000
4500
-2000
10000
-2130
7500
Array elements at odd position.1000 4500 10000 7500

By Finding Elements Divisible by 2 or not

To check the element present in an odd position, we can use the method divisible by 2 or not.

Example

import java.util.*;
public class PrintOddElementsInArray {
   public static void main(String[] args){
      int inputArray[] = new int[] { 1000, -5000, 4500, -2000, 10000, -2130, 7500 };
      System.out.println("Existing array elements ..");
      for (int i = 0; i < inputArray.length; i++) {
         System.out.println(inputArray[i]);
      }
      System.out.println(
      "Array elements at odd position.");     
      for (int i = 0; i < inputArray.length; i++) {
         if (i % 2 == 0) {
            System.out.println(inputArray[i]);
         }
      }
   }
}

Output

Existing array elements.
1000
-5000
4500
-2000
10000
-2130
7500
Array elements at odd position.
1000
4500
10000
7500

Conclusion

To find an element present in an odd position is more convenient process to apply the sorting method or apply the divisible by 2 methods. It evaluates the process of a particular condition is true or false.

In this article, we have learnt how to write a Java Program to return the elements at odd positions in a list by using these algorithms and examples.

Updated on: 31-Mar-2023

549 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements