Analysis of Algorithms | Big | Java program for the above approach ; Function to find whether a key exists in an array or not using linear search ; Traverse the given array , a [ ] ; Check if a [ i ] is equal to key ; Driver code ; Given Input ; Function Call
import java . lang . * ; import java . util . * ; class GFG { static boolean linearSearch ( int a [ ] , int n , int key ) { for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == key ) return true ; } return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 , 10 , 40 } ; int x = 10 ; int n = arr . length ; if ( linearSearch ( arr , n , x ) ) System . out . println ( " Element ▁ is ▁ present ▁ in ▁ array " ) ; else System . out . println ( " Element ▁ is ▁ not ▁ present ▁ in ▁ array " ) ; } }