Find the Index of an Array Element in Java
In Java, arrays are one of the most commonly used data structures for storing a collection of data. Here, we will find the position or you can index of a specific element in given array.
Example:
Input: a[] = { 5, 4, 6, 1, 3, 2, 7, 8, 9 }, element = 7 Output: 6
1. Using a Simple Loop
One of the simplest and most straightforward ways to find the index of an element in an array is by using a loop. You can iterate through the array and compare each element with the target element. When a match is found, you return the index.
// Java program to find index of array
// element using a while loop
import java.util.*;
public class Geeks {
// Linear-search function to find the index of an element
public static int findIndex(int a[], int t)
{
if (a == null)
return -1;
int len = a.length;
int i = 0;
// traverse in the array
while (i < len) {
// if the i-th element is t
// then return the index
if (a[i] == t) {
return i;
}
else {
i = i + 1;
}
}
return -1;
}
public static void main(String[] args)
{
int[] a = { 5, 4, 6, 1, 3, 2, 7, 8, 9 };
int t = 7;
// find the index of 7
System.out.println(findIndex(a, t));
}
}
Output
6
Here, we have used linear search in an array, the element can be found in O(N) complexity.
2. Using Arrays.binarySearch() for Sorted Arrays
Arrays.binarySearch() method can also be used in case if array is already sorted. This will be faster method than above one as this has time complexity of O(log n).
// Java program to find index of an array
// element using Arrays.binarySearch()
import java.util.Arrays;
public class Geeks{
public static int findIndex(int arr[], int t){
int index = Arrays.binarySearch(arr, t);
return (index < 0) ? -1 : index;
}
public static void main(String[] args){
int[] a = { 1, 2, 3, 4, 5, 6, 7 };
int t = 7;
// find the index of 5
System.out.println(findIndex(a, t));
}
}
Output
6
3. Using Guava Library
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.
// Java program to find index of an array
// element using Guava Library
import java.util.List;
import com.google.common.primitives.Ints;
public class Geeks{
// Function to find the index of an element using
public static int findIndex(int a[], int t){
return Ints.indexOf(a, t);
}
public static void main(String[] args){
int[] a = { 5, 4, 6, 1, 3, 2, 7, 8, 9 };
int t=5;
System.out.println(findIndex(a, t));
}
}
Output:
6
4. Using 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.
// Java program to find index of an array
// element using Stream API
import java.util.stream.IntStream;
public class Geeks {
// 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[] a = { 5, 4, 6, 1, 3, 2, 7, 8, 9 };
int t = 7;
System.out.println(findIndex(a, t));
}
}
Output
6
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 program to find index of an array
// element using ArrayList indexOf() Method
import java.util.ArrayList;
public class Geeks {
public static int findIndex(int arr[], int t){
ArrayList<Integer> clist = new ArrayList<>();
// adding elements of array
// to ArrayList
for (int i : arr)
clist.add(i);
return clist.indexOf(t);
}
public static void main(String[] args){
int[] a = { 5, 4, 6, 1, 3, 2, 7, 8, 9 };
int t = 7;
System.out.println(findIndex(a, t));
}
}
Output
6
6. Using Recursion
We will use recursion to find the first index of the given element.
// Java program to find index of an array
// element using Recursion
public class Geeks {
public static int index(int arr[], int t, int start){
if (start == arr.length)
return -1;
// if element at index start equals t
// we return start
if (arr[start] == t)
return start;
return index(arr, t, start + 1);
}
public static int findIndex(int arr[], int t){
return index(arr, t, 0);
}
public static void main(String[] args)
{
int[] a = { 5, 4, 6, 1, 3, 2, 7, 8, 9 };
int t = 7;
System.out.println(findIndex(a, t));
}
}
Output:
Index position of 5 is: 0
Index position of 7 is: 6