Linear Search

 

You have been given a random integer array/list(ARR) of size N, and an integer X. You need to search for the integer X in the given array/list using 'Linear Search'.

Sample Input 1:
7
2 13 4 1 3 6 28
3
Sample Output 1:
4

public class Solution {

    public static int linearSearch(int arr[], int x) {
		
        int l=arr.length;
        int pos=0;
        for(int i=0;i<l;i++)
        {
            if(arr[i]==x)
            {
                pos=i;
            }
        }
        if(pos==0)
        {
            return -1;
        }
        return pos;
    }
}

Comments

Popular posts from this blog

Minimum Length Word

Check Number Sequence

Star Pattern