Side Navigation


Linear Search

Write a method that returns the index of the first occurrence of a given integer in a list.
Assume that the index of the first element in the list is zero.
If the number does not exist return -1.
public class BestOnlineLearning {

    public static int linearSearch(int n, int[] list){
        int index = -1;
        for(int i= 0; i < list.length; i++){
            if(list[i] == n){
                index = i;
        return index;

    public static void main(String[] args) {
        int[] list = {1,2,3,4,5,6,7,8,9,4,5,6};
        linearSearch(4, list);


You May Also Like


Leave a Comment

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>