Linear Search in Java: Explanation & Practice

Find an element in an array

Problem summary

Search for element 30 in array [10, 20, 30, 40, 50] and print its index.

Starter code

public class Main {
    public static void main(String[] args) {
        int[] arr = {10, 20, 30, 40, 50};
        int target = 30;
        
        // Find index of target
        // Print: Found at index: <index> OR Not found
    }
}

Expected output and test cases

  • Find 30 in array
    Found at index: 2

Hints

  1. Loop through each element
  2. Compare with target value
  3. Return index when found

Related Arrays exercises

Practice all Arrays exercises · Run this idea in the Java compiler