Find The Index of An Array Element in Java10 Sept 2024 | 10 min read 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. 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 SearchThe 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 SearchThe 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 LibraryThe 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 APIStream 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 ArrayListWe 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 RecursionUsing 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. |
? In Java, removing a substring from a string involves manipulating the original string to exclude the specified substring. This process can be achieved by various means, typically involving string handling methods that identify the position of the substring and then create a new string without the...
10 min read
The pencil pattern is another pattern created from asterisk symbols using loop and other logical concepts. It is usually asked to draw pattern through a program. We use the following approach to write the code for it: Take input from the user and store it into a variable,...
4 min read
Eclipse is one of the most outstanding IDEs for Java and Android designers. There are many issues with Eclipse on the off chance that it isn't arranged as expected. You want to endeavour all of them individually. A few clients are kicking the Java Was started...
5 min read
Playfair cipher is proposed by Charles Whetstone in 1889. But it was named for one of his friends Lord Lyon Playfair because he popularized its uses. It is the most popular symmetric encryption technique that falls under the substitution cipher. It is an encoding procedure that...
9 min read
In this section, we will see how one can compute the maximum rectangular area in the histogram. What is maximum rectangular area in the histogram? The maximum rectangle that has to be created should be made of continuous bars. For the sake of simplicity, we will assume that...
10 min read
Given two sorted integer arrays, nums1 and nums2, and an integer k. The task is to determine the kth (-based) least product of nums1[i] * nums2[j], where 0 <= i < nums1.length and 0 <= j < nums2.length. Example 1: Input: nums1 = [2,8], nums2 = [3,4,5], k =...
6 min read
The Euclidean algorithm or the algorithm of descent is a well-established method of mathematics that is applied to find the GCD. The GCD stands for the largest equal divisor and is a positive integer. It divides both numbers without a remainder. Its use is essential...
4 min read
Java HashMap does not preserve any order by default. If there is a need to sort HashMap we sort it explicitly based on the requirements. Java provides an option to sort HashMap based on keys and values. In this section, we will learn how to sort...
4 min read
Sometimes, we need to convert a list of characters into a string. A string is a sequence of characters, so we can easily form a string from a character array. We have some special strings, such as a palindrome string (a string that is the same...
4 min read
? In Java, there are mainly three classes related to the String. The classes are String, StringBuilder, and StringBuffer class. These three classes provide methods related to string manipulation. Removing the first and last character from the String is also an operation that we can perform on...
6 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India