Open In App

Find the index of an array element in Java

Improve
Improve
Like Article
Like
Save
Share
Report

Given an array of N elements and an element K, find the index of an array element in Java. 
Examples: 

Input: a[] = { 5, 4, 6, 1, 3, 2, 7, 8, 9 }, K = 5
Output: 0

Input: a[] = { 5, 4, 6, 1, 3, 2, 7, 8, 9 }, K = 7
Output: 6

An element in an array of N integers can be searched using the below-mentioned methods.  

  1. Linear Search: Doing a linear search in an array, the element can be found in O(N) complexity. 
    Below is the implementation of the linear-search approach:

Java




// Java program to find index of
// an element in N elements
import java.util.*;
public class index {
  
    // Linear-search function to find the index of an element
    public static int findIndex(int arr[], int t)
    {
  
        // if array is Null
        if (arr == null) {
            return -1;
        }
  
        // find length of array
        int len = arr.length;
        int i = 0;
  
        // traverse in the array
        while (i < len) {
  
            // if the i-th element is t
            // then return the index
            if (arr[i] == t) {
                return i;
            }
            else {
                i = i + 1;
            }
        }
        return -1;
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        int[] my_array = { 5, 4, 6, 1, 3, 2, 7, 8, 9 };
  
        // find the index of 5
        System.out.println("Index position of 5 is: "
                           + findIndex(my_array, 5));
  
        // find the index of 7
        System.out.println("Index position of 7 is: "
                           + findIndex(my_array, 7));
    }
}


Output: 

Index position of 5 is: 0
Index position of 7 is: 6

 

Time Complexity: O(N)
Auxiliary Space: O(1)

2. Binary search: Binary search can also be used to find the index of the array element in an array. But the binary search can only be used if the array is sorted. Java provides us with an inbuilt function which can be found in the Arrays library of Java which will return the index if the element is present, else it returns -1. The complexity will be O(log n). 
Below is the implementation of Binary search. 

 

Java




// Java program to find index of
// an element in N elements
import java.util.Arrays;
  
public class index {
  
    // Function to find the index of an element
    public static int findIndex(int arr[], int t)
    {
  
        int index = Arrays.binarySearch(arr, t);
        return (index < 0) ? -1 : index;
    }
    // Driver Code
    public static void main(String[] args)
    {
        int[] my_array = { 1, 2, 3, 4, 5, 6, 7 };
  
        // find the index of 5
        System.out.println("Index position of 5 is: "
                           + findIndex(my_array, 5));
  
        // find the index of 7
        System.out.println("Index position of 7 is: "
                           + findIndex(my_array, 7));
    }
}


Output: 

Index position of 5 is: 4
Index position of 7 is: 6

 

Time Complexity: O(log N)
Auxiliary Space: O(1)

3. Guava: Guava is an open source, Java-based library developed by Google. It provides utility methods for collections, caching, primitives support, concurrency, common annotations, string processing, I/O, and validations. Guava provides several-utility class pertaining to be primitive like Ints for int, Longs for long, Doubles for double etc. Each utility class has an indexOf() method that returns the index of the first appearance of the element in array.

Below is the implementation of Guava.

Java




// Java program to find index of
// an element in N elements
import java.util.List;
import com.google.common.primitives.Ints;
public class index {
    // Function to find the index of an element using
    public static int findIndex(int arr[], int t)
    {
        return Ints.indexOf(arr, t);
    }
    // Driver Code
    public static void main(String[] args)
    {
        int[] my_array = { 5, 4, 6, 1, 3, 2, 7, 8, 9 };
        System.out.println("Index position of 5 is: "
                           + findIndex(my_array, 5));
        System.out.println("Index position of 7 is: "
                           + findIndex(my_array, 7));
    }
}


Output: 

Index position of 5 is: 0
Index position of 7 is: 6

 

Time Complexity: O(N)
Auxiliary Space: O(1)

4. Stream API: Stream is a new abstract layer introduced in Java 8. Using stream, you can process data in a declarative way similar to SQL statements. The stream represents a sequence of objects from a source, which supports aggregate operations. In order to find the index of an element Stream package provides utility, IntStream. Using the length of an array we can get an IntStream of array indices from 0 to n-1, where n is the length of an array.
Below is the implementation of Stream API approach. 

Java




// Java program to find index of
// an element in N elements
import java.util.stream.IntStream;
public class index {
  
    // Function to find the index of an element
    public static int findIndex(int arr[], int t)
    {
        int len = arr.length;
        return IntStream.range(0, len)
            .filter(i -> t == arr[i])
            .findFirst() // first occurrence
            .orElse(-1); // No element found
    }
    public static void main(String[] args)
    {
        int[] my_array = { 5, 4, 6, 1, 3, 2, 7, 8, 9 };
        System.out.println("Index position of 5 is: "
                           + findIndex(my_array, 5));
        System.out.println("Index position of 7 is: "
                           + findIndex(my_array, 7));
    }
}


Output: 

Index position of 5 is: 0
Index position of 7 is: 6

 

Time Complexity: O(N)
Auxiliary Space: O(1)

5. Using ArrayList

In this approach, we will convert the array into ArrayList, and then we will use the indexOf method of ArrayList to get the index of the element.

Java




// Java program for the above approach
import java.util.ArrayList;
  
public class GFG {
  
    public static int findIndex(int arr[], int t)
    {
        // Creating ArrayList
        ArrayList<Integer> clist = new ArrayList<>();
  
        // adding elements of array
        // to ArrayList
        for (int i : arr)
            clist.add(i);
  
        // returning index of the element
        return clist.indexOf(t);
    }
    
    // Driver program
    public static void main(String[] args)
    {
        int[] my_array = { 5, 4, 6, 1, 3, 2, 7, 8, 9 };
        System.out.println("Index position of 5 is: "
                           + findIndex(my_array, 5));
        System.out.println("Index position of 7 is: "
                           + findIndex(my_array, 7));
    }
}


Output:

Index position of 5 is: 0
Index position of 7 is: 6

Time Complexity: O(N)
Auxiliary Space: O(N)

6. Using Recursion

We will use recursion to find the first index of the given element.

Java




// Java program for the above approach
public class GFG {
  
    public static int index(int arr[], int t, int start)
    {
          
        // base case when 
        // start equals the length of the 
        // array we return -1
        if(start==arr.length)
            return -1;
          
        // if element at index start equals t
        // we return start
        if(arr[start]==t)
            return start;
          
        // we find for the rest
        // position in the array
        return index(arr,t,start+1);
    }
    
    public static int findIndex(int arr[], int t)
    {
        return index(arr,t,0);
    }
    
    // Driver Code
    public static void main(String[] args)
    {
        int[] my_array = { 5, 4, 6, 1, 3, 2, 7, 8, 9 };
        System.out.println("Index position of 5 is: "
                + findIndex(my_array, 5));
        System.out.println("Index position of 7 is: "
                + findIndex(my_array, 7));
    }
}


Output:

Index position of 5 is: 0
Index position of 7 is: 6

Time Complexity: O(N)
Auxiliary Space: O(1)



Last Updated : 20 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads