Return Elements at Odd Position in a List in Java



What is the Odd Position in a List?

In Java, a List does not have predefined odd and even positions. However, if one considers the first position as index 0, then an odd position in the given List would be any index that is not divisible by 2. To determine if a position is odd, use the modulo operator (%).

 

How to Find Elements at Odd Position in a List?

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

  • By dividing index with 2
  • By incrementing index by 2
  • By maintaining flag pointer

By Dividing Index with 2

In this approach, follow the below steps ?

  • Create and initialize a list with some values.
  • Use for-each loop to iterate through the list.
  • For every element, checks whether its position is odd or not.
  • If the position is odd, print the value.

Example

Following Java program shows how to find elements at the odd position in a list.

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;
      }
   }
}

On executing, this code will produce following result ?

Elements present at odd position are: 200 400 600

By Incrementing Index Position by 2

Steps to be followed in this approach ?

  • Traverse list from the first position.
  • Increment position by 2 with each iteration.
  • Print the value.

Example

The following example practically demonstrates the approach discussed above.

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) + " ");
      }
   }
}

When you execute this code, it will show below result ?

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

By using Flag Pointer

Steps to get an odd element using flag pointer are ?

  • Start

  • Pointer initialized to FALSE.

  • Start iteration using for-each.

  • If the flag is TRUE, then print the data.

  • Change flag to FALSE.

  • Repeat this process till the end of the list.

  • End

Example

In this example, we are finding the odd position element by using flag pointer.

import java.util.*;
public class PrintOddElementsInArrayList {
   public static void main(String[] args) {
      // create and initialize arraylist
      List<Integer> inputList = new ArrayList<>();
      inputList.add(1000);
      inputList.add(-5000);
      inputList.add(4500);
      inputList.add(-2000);
      inputList.add(10000);
      inputList.add(-2130);
      inputList.add(7500);
      // loop to print elements of the list
      System.out.println("Existing list elements:");
      for (Integer element : inputList) {
         System.out.println(element);
      }
      // printing elements at the odd position
      System.out.println("Elements at odd positions:");
      boolean flag = false;
      for (Integer element : inputList) {
         if (flag) {
            System.out.print(element + " ");
         }
         // Toggle flag for odd positions
         flag = !flag; 
      }
   }
}

On running the above code, following output will be displayed ?

Existing list elements:
1000
-5000
4500
-2000
10000
-2130
7500
Elements at odd positions:
-5000 -2000 -2130 
Updated on: 2024-08-01T11:43:36+05:30

865 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements