# Find the index of an array element in Java

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 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 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 rreturn 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 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 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 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 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 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
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : nidhi_biet

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.