Javatpoint Logo
Javatpoint Logo

Find The Index of An Array Element in Java

In this tutorial, we will see how one finds the index of an array element in Java. To avoid confusion, we will assume that all the elements of the array are unique. In other words, no element will occur more than once. In the input, an array and a number k will be given to us. We have to have the index of the element k in the array. If the number k is not there in the array, an appropriate message should be displayed. Consider zero indexing.

Example 1:

Input

int arr[] = {4, 3, 6, 7, 10, 17, 13, 19}, k = 7

Output: Index of element 7 is 3.

Explanation: Starting from the left, the index of the element 7 is 3.

Example 2:

Input

int arr[] = {67, 23, 89, 30, 35, 1, 69, 90, 43, 61}, k = 11

Output: Element 11 is not found in the input array.

Explanation: Element 11 is absent in the input array. Hence, it is not possible to find its index.

Approach: Linear Search

The linear search approach is simple. Using a loop, iterate through the array and check when element k is found or not. If found, return its index. The value contained in the loop variable at the time will be the index of the element k. If the element is not found, even after the loop is over, display an appropriate message.

FileName: ArrayElementIndexRecursion.java

Output:

For the following array: 
4 3 6 7 10 17 13 19 
The element 7 is found at the index 3


For the following array: 
67 23 89 30 35 1 69 90 43 61 
The element 11 is not found.

Complexity Analysis: The time complexity of the program is O(n), where n is the total number of elements present in the input array. The space complexity of the program is O(1), as there is no extra space used in the program.

Approach: Using Binary Search

The binary search approach works only when the input array is sorted either in ascending or descending order. The binary search is better than the linear search in terms of complexity.

FileName: ArrayElementIndexBinarySearch.java

Output:

For the following array: 
4 3 6 7 10 13 17 19 
The element 7 is found at the index 3


For the following array: 
1 23 30 35 43 61 67 69 89 90 
The element 11 is not found.

Complexity Analysis: The time complexity of the program is O(log(n)), where n is the total number of elements present in the input array. The space complexity of the program is O(1), as there is no extra space used in the program.

Approach: Using Guava Library

The Guava Library is a library that is developed by Google engineers. Guava Library provides various utility classes pertaining to primitives like Longs for long, Doubles for double. Ints for int, etc. Each utility class has the indexOf() method that returns the index of the first occurrence of the element in the array.

FileName: ArrayElementIndexGuavaLib.java

Output:

For the following array:
4 3 6 7 10 17 13 19
The element 7 is found at the index 3


For the following array:
67 23 89 30 35 1 69 90 43 61
The element 11 is not found.

Explanation: The import statement works well only when we include the guava.jar file in the classpath. On our side, we have compiled and run the program like the following.

For the compilation process, we have used the following:

javac -cp ".;/guava.jar" ArrayElementIndexGuavaLib.java

For running the program, we have used:

java -cp ".;/guava.jar" ArrayElementIndexGuavaLib.

The guava.jar file can be downloaded from here.

Complexity Analysis: The time complexity of the program is O(N), as Ints.indexOf takes O(N) time, where N is the total number of elements present in the array. The space complexity of the program is O(1).

Approach: Using Stream API

Stream is the new abstract layer that is introduced in Java 8. In the stream, one can do the processing of data in a declarative way similar to what we do for the SQL statements. Using the IntStream utility of the Stream package, one can find the index of an element. See the following.

FileName: ArrayElementIndexStream.java

Output:

For the following array:
4 3 6 7 10 17 13 19
The element 7 is found at the index 3


For the following array:
67 23 89 30 35 1 69 90 43 61
The element 11 is not found.

Complexity Analysis: The time, as well as the space complexity of the program, is the same as the previous program.

Approach: Using ArrayList

We can use the inbuilt methods of the ArrayList class to achieve our goal. The indexOf() method of the array list comes in handy.

FileName: ArrayElementIndexArrayList.java

Output:

For the following array:
4 3 6 7 10 17 13 19
The element 7 is found at the index 3


For the following array:
67 23 89 30 35 1 69 90 43 61
The element 11 is not found.

Complexity Analysis: The time complexity of the program is O(N), because of the single for-loop used in the program. The space complexity of the program is also O(N), because we are using an array list for storing the elements of the input array. N is the total number of elements present in the input array.

Approach: Using Recursion

Using recursion, one can also find the index of the element. The iteration of the elements is done with the help of recursion.

FileName: ArrayElementIndexArrayList.java

Output:

For the following array:
4 3 6 7 10 17 13 19
The element 7 is found at the index 3


For the following array:
67 23 89 30 35 1 69 90 43 61
The element 11 is not found.

Complexity Analysis: The time complexity of the program is O(N), because of the single for-loop used in the program. The space complexity of the program is also O(N), because we are using an array list for storing the elements of the input array. N is the total number of elements present in the input array.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA