Java Program to Recursively Linearly Search an Element in an Array



Linear Search on a Java Array using Recursively

Linear search is an algorithm that we use to check if an element is present in an array or not. It checks all the elements of the array one by one until it finds the element or reaches the end of the array.

Recursion is a way where a function calls itself to solve a certain problem until it reaches a base case. In this article, we will understand how to recursively linearly search an element in an array. Let's see an example:

Input:
int [] arr = {1, 2, 3, 4, 5};
int target = 3;

Output:
Element 3 found at index 2

Algorithm

  • Instantiate an array with some elements and a target element to search.
  • Create a recursive function that takes the array, the target element, and the current index as parameters.
  • In the recursive function, check if the current index is equal to the length of the array. If it is, return -1, indicating that the element was not found.
  • Check if the element at the current index is equal to the target element. If it is, return the current index.
  • Otherwise, call the recursive function with the next index (current index + 1).

Example 1

In this example, we will search for an integer element in an array of integers using recursion.

public class RecursiveLinearSearch {
   public static void main(String[] args){
      int[] arr = {8,5,3,24,534,23,4};
      int target = 24;
      int ind = linearSearch(arr, target, 0);
      if (ind != -1) {
         System.out.println("Element " + target + " found at index " + ind);
      } else {
         System.out.println("Element " + target + " not found in the array");
      }
   }
   public static int linearSearch(int[] arr, int target, int ind){
      //Base case
      if(ind == arr.length) {
         return -1; 
      }
      if(arr[ind] == target) {
         return ind; 
      }
      return linearSearch(arr, target, ind + 1);
   }
}

Output

Following is the output of the above code:

Element 24 found at index 3

Example 2

In this example, we will search for a string element in an array of strings using recursion.

public class RecursiveLinearSearch {
   public static void main(String[] args){
      String[] arr = {"apple", "banana", "cherry", "date", "fig"};
      String target = "cherry";
      int ind = linearSearch(arr, target, 0);
      if (ind != -1) {
         System.out.println("Element " + target + " found at index " + ind);
      } else {
         System.out.println("Element " + target + " not found in the array");
      }
   }
   public static int linearSearch(String[] arr, String target, int ind){
      //Base case
      if(ind == arr.length) {
         return -1; 
      }
      if(arr[ind].equals(target)) {
         return ind; 
      }
      return linearSearch(arr, target, ind + 1);
   }
}

Output

Following is the output of the above code:

Element cherry found at index 2
Aishwarya Naglot
Aishwarya Naglot

Writing clean code… when the bugs aren’t looking.

Updated on: 2025-06-05T14:59:33+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements