Find index of an element in given array in Java
This post will discuss how to find the index of an element in a primitive or object array in Java.
The solution should either return the index of the first occurrence of the required element or -1 if it is not present in the array.
1. Naive Solution – Linear search
A naive solution is to perform a linear search on the given array to determine whether the target element is present in the array.
⮚ For primitive arrays:
1 2 3 4 5 6 7 8 9 10 11 12 |
// Method to find the index of an element in a primitive array in Java public static int find(int[] a, int target) { for (int i = 0; i < a.length; i++) { if (a[i] == target) { return i; } } return -1; } |
⮚ For object arrays:
1 2 3 4 5 6 7 8 9 10 11 12 |
// Generic method to find the index of an element in an object array in Java public static<T> int find(T[] a, T target) { for (int i = 0; i < a.length; i++) { if (target.equals(a[i])) { return i; } } return -1; } |
2. Using Java 8 Stream
We can use Java 8 Stream to find the index of an element in a primitive and object array, as shown below:
⮚ For primitive arrays:
1 2 3 4 5 6 7 8 |
// Method to find the index of an element in a primitive array in Java public static int find(int[] a, int target) { return IntStream.range(0, a.length) .filter(i -> target == a[i]) .findFirst() .orElse(-1); // return -1 if the target is not found } |
⮚ For object arrays:
1 2 3 4 5 6 7 8 |
// Generic method to find the index of an element in an object array in Java public static<T> int find(T[] a, T target) { return IntStream.range(0, a.length) .filter(i -> target.equals(a[i])) .findFirst() .orElse(-1); // return -1 if the target is not found } |
3. Convert To List
The idea is to convert the given array to a list and use the List.indexOf()
method that returns the index of the first occurrence of the specified element in this list.
⮚ For primitive arrays:
How to convert primitive integer array to List<Integer>?
1 2 3 4 5 6 7 8 |
// Method to find the index of an element in a primitive array in Java public static int find(int[] a, int target) { return Arrays.stream(a) // IntStream .boxed() // Stream<Integer> .collect(Collectors.toList()) // List<Integer> .indexOf(target); } |
⮚ For object arrays:
How to convert an object array to a list?
1 2 3 4 |
// Generic method to find the index of an element in an object array in Java public static<T> int find(T[] a, T target) { return Arrays.asList(a).indexOf(target); } |
4. Binary search for Sorted Arrays
For sorted arrays, we can use Binary Search Algorithm to search the specified array for the specified value.
⮚ For primitive arrays:
1 2 3 4 5 6 |
// Method to find the index of an element in a primitive array in Java public static int find(int[] a, int target) { int index = Arrays.binarySearch(a, target); return (index < 0) ? -1 : index; } |
⮚ For object arrays:
1 2 3 4 5 6 |
// Generic method to find the index of an element in an object array in Java public static<T> int find(T[] a, T target) { int index = Arrays.binarySearch(a, target); return (index < 0) ? -1 : index; } |
5. Using Guava Library
⮚ For primitive arrays:
Guava library provides several utility classes pertaining to primitives, like Ints for int, Longs
for long, Doubles
for double, Floats
for float, Booleans
for boolean, and so on.
Each utility class has the indexOf()
method that returns the index of the first appearance of the target in the array. We can also use lastIndexOf()
to return the index of the target’s last appearance in the array.
1 2 3 4 |
// Method to find the index of an element in a primitive array in Java public static int find(int[] a, int target) { return Ints.indexOf(a, target); } |
⮚ For object arrays:
Guava’s com.google.commons.collect.Iterables class contains static utility method indexOf(Iterator, Predicate)
that returns the index of the first element that satisfies the provided predicate, or -1 if the iterator has no such elements.
1 2 3 4 5 6 7 8 9 10 11 12 |
// Generic method to find the index of an element in an object array in Java public static<T> int find(T[] a, T target) { int index = Iterators.indexOf(Iterators.forArray(a), new Predicate<T>() { public boolean apply(T input) { return input.equals(target); } }); return index; } |
We can also use the com.google.common.base.Predicates class provided by Guava that contains static utility methods pertaining to Predicate
instances.
1 2 3 4 5 6 7 |
// Generic method to find the index of an element in an object array in Java public static<T> int find(T[] a, T target) { int index = Iterators.indexOf(Iterators.forArray(a), Predicates.in(Collections.singleton(target))); return index; } |
For Java 8 and above, we can use lambda expressions:
1 2 3 4 |
// Generic method to find the index of an element in an object array in Java public static<T> int find(T[] a, T target) { return Iterators.indexOf(Iterators.forArray(a), x -> x.equals(target)); } |
6. Using Apache Commons Lang
Apache Commons Lang ArrayUtils class contains several static utility methods that operate on primitive or object arrays. It provides the indexOf()
method that finds the index of the given value in the array.
⮚ For primitive arrays:
1 2 3 4 |
// Method to find the index of an element in a primitive array in Java public static int find(int[] a, int target) { return ArrayUtils.indexOf(a, target); } |
⮚ For object arrays:
1 2 3 4 |
// Generic method to find the index of an element in an object array in Java public static<T> int find(T[] a, T target) { return ArrayUtils.indexOf(a, target); } |
That’s all about finding the index of an element in an array in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)