Find the index of an array element in Java
Last Updated :
20 Feb, 2023
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.
- 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
import java.util.*;
public class index {
public static int findIndex( int arr[], int t)
{
if (arr == null ) {
return - 1 ;
}
int len = arr.length;
int i = 0 ;
while (i < len) {
if (arr[i] == t) {
return i;
}
else {
i = i + 1 ;
}
}
return - 1 ;
}
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)
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
import java.util.Arrays;
public class index {
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 [] my_array = { 1 , 2 , 3 , 4 , 5 , 6 , 7 };
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: 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
import java.util.List;
import com.google.common.primitives.Ints;
public class index {
public static int findIndex( int arr[], int t)
{
return Ints.indexOf(arr, t);
}
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
import java.util.stream.IntStream;
public class index {
public static int findIndex( int arr[], int t)
{
int len = arr.length;
return IntStream.range( 0 , len)
.filter(i -> t == arr[i])
.findFirst()
.orElse(- 1 );
}
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
import java.util.ArrayList;
public class GFG {
public static int findIndex( int arr[], int t)
{
ArrayList<Integer> clist = new ArrayList<>();
for ( int i : arr)
clist.add(i);
return clist.indexOf(t);
}
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
public class GFG {
public static int index( int arr[], int t, int start)
{
if (start==arr.length)
return - 1 ;
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 [] 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)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...