Analysis of Algorithms | Big | C # program for 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
using System ; class GFG { static bool linearSearch ( int [ ] a , int n , int key ) { for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == key ) return true ; } return false ; } static void Main ( ) { int [ ] arr = { 2 , 3 , 4 , 10 , 40 } ; int x = 10 ; int n = arr . Length ; if ( linearSearch ( arr , n , x ) ) Console . Write ( " Element ▁ is ▁ present ▁ in ▁ array " ) ; else Console . Write ( " Element ▁ is ▁ not ▁ present ▁ in ▁ array " ) ; } }