Java Find First Occurrence in Array

Java Find First Occurrence in Array | In this post we will discuss how to find the first occurrence of an element in the given array. The code will return the index of the first occurrence of the specified element. To know more in detail see the below examples:- 

1) Array = {10,20,39,89}
Element = 89
The first occurrence of 89 is at the 3 indexes.

2) Array = {9,9,8,7}
Element = 9
The first occurrence of 9 is at the 0 indexes.

3) Array = {0,9,8,6}
Element = 1
Not found.

Observe the above example there are three test cases. First where there is an occurrence of the element is only once, and in the second one there are two occurrences of the given element but it retrieves only the first occurrence and in the third one there is no occurrence of the element hence it returns not found. 

Java Find First Occurrence in Array Program

Let us see the Java Find First Occurrence in Array. Here we will take the array as input from the end-user. We will use the linear search algorithm to find the first occurrence of an element in the given array. Also see:- Linear Search in Java.

Java Find First Occurrence in Array Program

import java.util.Arrays;
import java.util.Scanner;

public class Main {

   public static int findFirst(int[] nums, int target) {
      for (int i = 0; i < nums.length; i++) {
         if (nums[i] == target) {
            return i;
         }
      }
      return -1;
   }

   public static void main(String[] args) {
      Scanner scan = new Scanner(System.in);
      System.out.print("Enter the number of elements in the array: ");
      int n = scan.nextInt();

      int[] nums = new int[n];
      System.out.println("Enter the elements of the array: ");
      for (int i = 0; i < n; i++) {
         nums[i] = scan.nextInt();
      }

      System.out.println("Array = " + Arrays.toString(nums));
      System.out.print("Enter the element to find the occurrence: ");
      int target = scan.nextInt();

      int index = findFirst(nums, target);
      if (index != -1) {
         System.out.println("The first occurrence of element " 
                  + target + " is located at index " + index);
      } else {
         System.out.println("Element " + target 
               + " is not found in the given array.");
      }
      scan.close();
   }
}

Output:-

Enter the number of elements in the array: 5
Enter the elements of the array:
10 20 30 40 50
Array = [10, 20, 30, 40, 50]
Enter the element to find the occurrence: 30
The first occurrence of element 30 is located at index 2

Test case where there is more than one occurrence of the target. 

Enter the number of elements in the array: 7
Enter the elements of the array:
21 23 21 25 23 21 27
Array = [21, 23, 21, 25, 23, 21, 27]
Enter the element to find the occurrence: 23
The first occurrence of element 23 is located at index 1

Test case where there is no occurrence of the target.

Enter the number of elements in the array: 5
Enter the elements of the array:
10 20 15 10 20
Array = [10, 20, 15, 10, 20]
Enter the element to find the occurrence: 50
Element 50 is not found in the given array.

In the Java Find First Occurrence in Array program, we have used the Arrays.toString() method to display the array elements. Java Arrays class contains multiple methods like Arrays.sort(), Arrays.copyof(), Arrays.copyOfRange(), Arrays.fill(), Arrays.equals(), and more to solve array related common problems. Also see:- Different ways to Print Array

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *