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.
- 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 elementsimportjava.util.*;publicclassindex {// Linear-search function to find the index of an elementpublicstaticintfindIndex(intarr[],intt){// if array is Nullif(arr ==null) {return-1;}// find length of arrayintlen = arr.length;inti =0;// traverse in the arraywhile(i < len) {// if the i-th element is t// then return the indexif(arr[i] == t) {returni;}else{i = i +1;}}return-1;}// Driver Codepublicstaticvoidmain(String[] args){int[] my_array = {5,4,6,1,3,2,7,8,9};// find the index of 5System.out.println("Index position of 5 is: "+ findIndex(my_array,5));// find the index of 7System.out.println("Index position of 7 is: "+ findIndex(my_array,7));}}chevron_rightfilter_noneOutput:
Index position of 5 is: 0 Index position of 7 is: 6
- 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 elementsimportjava.util.Arrays;publicclassindex {// Function to find the index of an elementpublicstaticintfindIndex(intarr[],intt){intindex = Arrays.binarySearch(arr, t);return(index <0) ? -1: index;}// Driver Codepublicstaticvoidmain(String[] args){int[] my_array = {1,2,3,4,5,6,7};// find the index of 5System.out.println("Index position of 5 is: "+ findIndex(my_array,5));// find the index of 7System.out.println("Index position of 7 is: "+ findIndex(my_array,7));}}chevron_rightfilter_noneOutput:Index position of 5 is: 4 Index position of 7 is: 6
- 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 elementsimportjava.util.List;importcom.google.common.primitives.Ints;publicclassindex {// Function to find the index of an element usingpublicstaticintfindIndex(intarr[],intt){returnInts.indexOf(arr, t);}// Driver Codepublicstaticvoidmain(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));}}chevron_rightfilter_noneOutput:Index position of 5 is: 0 Index position of 7 is: 6
- 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 elementsimportjava.util.stream.IntStream;publicclassindex {// Function to find the index of an elementpublicstaticintfindIndex(intarr[],intt){intlen = arr.length;returnIntStream.range(0, len).filter(i -> t == arr[i]).findFirst()// first occurence.orElse(-1);// No element found}publicstaticvoidmain(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));}}chevron_rightfilter_noneOutput:Index position of 5 is: 0 Index position of 7 is: 6
Recommended Posts:
- Find index of an extra element present in one sorted array
- Remove an Element at specific index from an Array in Java
- Delete array element in given index range [L - R]
- List add(int index, E element) method in Java
- Find the index of first 1 in a sorted array of 0's and 1's
- Find the index of first 1 in an infinite sorted array of 0s and 1s
- Find a Fixed Point (Value equal to index) in a given array
- Find index of first occurrence when an unsorted array is sorted
- Find a Fixed Point (Value equal to index) in a given array | Duplicates Allowed
- Find if array has an element whose value is half of array sum
- Find an element in Bitonic array
- Find a peak element in a 2D array
- Find an array element such that all elements are divisible by it
- Find the element whose multiplication with -1 makes array sum 0
- Find the element that appears once in a sorted array
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.



