Java program to find the index of an array element

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 SearchDoing a linear search in an array, the element are often found in O(N) complexity.


Below is that the implementation of the linear-search approach:
 
// 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

2. Binary searchBinary search can also be used to find the index of an array element in an array. However, binary search can only be used when the array is sorted. Java provides us with a built-in function, which can be found in Java's array library. If the element exists, it will return the index, otherwise it will return -1. The complexity is O(log n). The following is the implementation of binary search.

 

// 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

3. Guava: Guava is a Java-based open source library developed by Google. It provides practical methods for collections, caching, primitive support, concurrency, general annotations, string processing, I/O, and verification. Guava provides several utility classes related to primitive types, such as Ints for int, Longs for long, doubles for double, and so on. Each utility class has an indexOf() method, which returns the index of the element's first occurrence in the array. The following is the realization of Guava.

// 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

4. Stream API: Stream is a new abstraction layer introduced in Java 8. Using stream, you can process data in a declarative manner similar to SQL statements. A stream represents a series of objects from a source, and it supports aggregation operations. In order to find the index of an element, the Stream package provides a utility, IntStream. Using the length of the array, we can get an IntStream of the array index from 0 to n-1, where n is the length of the array. The following is the implementation of the Stream API method.

// 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

5. Using ArrayList: 

 
// Java program for the above approach
import java.util.ArrayList;

public class GFG {

	public static int findIndex(int arr[], int t)
	{
		// Creating ArrayList
		ArrayList 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

6. Using Recursion

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

 

 
// 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

 

Submit Your Programming Assignment Details