{
  "id": "searching/linear-search",
  "version": 1,
  "deterministic": true,
  "url": "/dsa/searching/linear-search",
  "topic": {
    "slug": "searching",
    "title": "Searching"
  },
  "title": "Linear Search",
  "tagline": "Check every box until you find it. Simple, honest, and slow.",
  "vizKind": "array",
  "complexity": {
    "time": {
      "best": "O(1)",
      "average": "O(n)",
      "worst": "O(n)"
    },
    "space": "O(1)",
    "growth": "linear",
    "note": "Best case: it's the very first box. Worst case: it's the last one, or not there at all — and you touched all n boxes to be sure."
  },
  "code": {
    "javascript": "function linearSearch(a, target) {\n  for (let i = 0; i < a.length; i++) {\n    if (a[i] === target) {\n      return i;\n    }\n  }\n  return -1;\n}",
    "python": "def linear_search(a, target):\n    for i in range(len(a)):\n        if a[i] == target:\n            return i\n\n    return -1\n",
    "java": "int linearSearch(int[] a, int target) {\n  for (int i = 0; i < a.length; i++) {\n    if (a[i] == target) {\n      return i;\n    }\n  }\n  return -1;\n}",
    "cpp": "int linearSearch(vector<int> a, int target) {\n  for (int i = 0; i < a.size(); i++) {\n    if (a[i] == target) {\n      return i;\n    }\n  }\n  return -1;\n}"
  },
  "defaultInput": [
    42,
    7,
    19,
    3,
    88,
    15,
    61
  ],
  "defaultTarget": 15,
  "inputHint": "Type a list, then the number to hunt for.",
  "pitfalls": [
    {
      "wrong": "Returning 0 when the value isn't found.",
      "right": "0 is a real index — it means 'found at the front'. Use −1 (or null) for 'not here', so the two answers can't be confused."
    },
    {
      "wrong": "Carrying on looping after you've already found it.",
      "right": "Return the moment you find it. Otherwise you do the full worst-case work every single time, even on a lucky hit."
    },
    {
      "wrong": "Reaching for linear search on a big, sorted list.",
      "right": "If it's sorted, binary search will find it in a handful of looks instead of thousands. Sorted data is a gift — use it."
    }
  ],
  "quiz": [
    {
      "q": "A list has 1,000 items and the one you want is last. How many do you check?",
      "options": [
        "1",
        "10",
        "500",
        "1,000"
      ],
      "answer": 3,
      "why": "Linear search checks from the front, so the last item is the worst case: all 1,000 get checked."
    },
    {
      "q": "What does linear search require of the list?",
      "options": [
        "It must be sorted",
        "It must contain no duplicates",
        "Nothing at all",
        "It must be numbers"
      ],
      "answer": 2,
      "why": "Nothing. That's its real strength — it works on any list in any order, which is exactly when you need it."
    },
    {
      "q": "You double the size of the list. What happens to the worst-case work?",
      "options": [
        "Stays the same",
        "Doubles",
        "Goes up by one",
        "Squares"
      ],
      "answer": 1,
      "why": "Twice the boxes means twice the checking. Cost grows in lockstep with size — that is O(n)."
    }
  ],
  "problems": [
    {
      "name": "Find the Index of the First Occurrence in a String",
      "difficulty": "Easy",
      "url": "https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string/"
    },
    {
      "name": "Missing Number",
      "difficulty": "Easy",
      "url": "https://leetcode.com/problems/missing-number/"
    },
    {
      "name": "Majority Element",
      "difficulty": "Easy",
      "url": "https://leetcode.com/problems/majority-element/"
    }
  ],
  "trace": {
    "note": "Each frame is one immutable step: data snapshot, pointers, per-index roles, executing code line, and a one-sentence explanation.",
    "length": 8,
    "frames": [
      {
        "data": [
          {
            "id": "c0-42",
            "value": 42
          },
          {
            "id": "c1-7",
            "value": 7
          },
          {
            "id": "c2-19",
            "value": 19
          },
          {
            "id": "c3-3",
            "value": 3
          },
          {
            "id": "c4-88",
            "value": 88
          },
          {
            "id": "c5-15",
            "value": 15
          },
          {
            "id": "c6-61",
            "value": 61
          }
        ],
        "pointers": [],
        "highlights": [],
        "codeLine": 1,
        "variables": {
          "target": 15
        },
        "explanation": "Looking for 15. Linear search is the honest, patient way: check every box from left to right until you find it.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c0-42",
            "value": 42
          },
          {
            "id": "c1-7",
            "value": 7
          },
          {
            "id": "c2-19",
            "value": 19
          },
          {
            "id": "c3-3",
            "value": 3
          },
          {
            "id": "c4-88",
            "value": 88
          },
          {
            "id": "c5-15",
            "value": 15
          },
          {
            "id": "c6-61",
            "value": 61
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 0
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "compare"
          }
        ],
        "codeLine": 3,
        "variables": {
          "i": 0,
          "target": 15,
          "looking_at": 42,
          "checked": 1
        },
        "explanation": "Box 0 holds 42, not 15. Move on.",
        "stats": {
          "comparisons": 1,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c0-42",
            "value": 42
          },
          {
            "id": "c1-7",
            "value": 7
          },
          {
            "id": "c2-19",
            "value": 19
          },
          {
            "id": "c3-3",
            "value": 3
          },
          {
            "id": "c4-88",
            "value": 88
          },
          {
            "id": "c5-15",
            "value": 15
          },
          {
            "id": "c6-61",
            "value": 61
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 1
          }
        ],
        "highlights": [
          {
            "index": 1,
            "role": "compare"
          },
          {
            "index": 0,
            "role": "discarded"
          }
        ],
        "codeLine": 3,
        "variables": {
          "i": 1,
          "target": 15,
          "looking_at": 7,
          "checked": 2
        },
        "explanation": "Box 1 holds 7, not 15. Move on.",
        "stats": {
          "comparisons": 2,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c0-42",
            "value": 42
          },
          {
            "id": "c1-7",
            "value": 7
          },
          {
            "id": "c2-19",
            "value": 19
          },
          {
            "id": "c3-3",
            "value": 3
          },
          {
            "id": "c4-88",
            "value": 88
          },
          {
            "id": "c5-15",
            "value": 15
          },
          {
            "id": "c6-61",
            "value": 61
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 2
          }
        ],
        "highlights": [
          {
            "index": 2,
            "role": "compare"
          },
          {
            "index": 0,
            "role": "discarded"
          },
          {
            "index": 1,
            "role": "discarded"
          }
        ],
        "codeLine": 3,
        "variables": {
          "i": 2,
          "target": 15,
          "looking_at": 19,
          "checked": 3
        },
        "explanation": "Box 2 holds 19, not 15. Move on.",
        "stats": {
          "comparisons": 3,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c0-42",
            "value": 42
          },
          {
            "id": "c1-7",
            "value": 7
          },
          {
            "id": "c2-19",
            "value": 19
          },
          {
            "id": "c3-3",
            "value": 3
          },
          {
            "id": "c4-88",
            "value": 88
          },
          {
            "id": "c5-15",
            "value": 15
          },
          {
            "id": "c6-61",
            "value": 61
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 3
          }
        ],
        "highlights": [
          {
            "index": 3,
            "role": "compare"
          },
          {
            "index": 0,
            "role": "discarded"
          },
          {
            "index": 1,
            "role": "discarded"
          },
          {
            "index": 2,
            "role": "discarded"
          }
        ],
        "codeLine": 3,
        "variables": {
          "i": 3,
          "target": 15,
          "looking_at": 3,
          "checked": 4
        },
        "explanation": "Box 3 holds 3, not 15. Move on.",
        "stats": {
          "comparisons": 4,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c0-42",
            "value": 42
          },
          {
            "id": "c1-7",
            "value": 7
          },
          {
            "id": "c2-19",
            "value": 19
          },
          {
            "id": "c3-3",
            "value": 3
          },
          {
            "id": "c4-88",
            "value": 88
          },
          {
            "id": "c5-15",
            "value": 15
          },
          {
            "id": "c6-61",
            "value": 61
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 4
          }
        ],
        "highlights": [
          {
            "index": 4,
            "role": "compare"
          },
          {
            "index": 0,
            "role": "discarded"
          },
          {
            "index": 1,
            "role": "discarded"
          },
          {
            "index": 2,
            "role": "discarded"
          },
          {
            "index": 3,
            "role": "discarded"
          }
        ],
        "codeLine": 3,
        "variables": {
          "i": 4,
          "target": 15,
          "looking_at": 88,
          "checked": 5
        },
        "explanation": "Box 4 holds 88, not 15. Move on.",
        "stats": {
          "comparisons": 5,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c0-42",
            "value": 42
          },
          {
            "id": "c1-7",
            "value": 7
          },
          {
            "id": "c2-19",
            "value": 19
          },
          {
            "id": "c3-3",
            "value": 3
          },
          {
            "id": "c4-88",
            "value": 88
          },
          {
            "id": "c5-15",
            "value": 15
          },
          {
            "id": "c6-61",
            "value": 61
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 5
          }
        ],
        "highlights": [
          {
            "index": 5,
            "role": "found"
          },
          {
            "index": 0,
            "role": "discarded"
          },
          {
            "index": 1,
            "role": "discarded"
          },
          {
            "index": 2,
            "role": "discarded"
          },
          {
            "index": 3,
            "role": "discarded"
          },
          {
            "index": 4,
            "role": "discarded"
          }
        ],
        "codeLine": 3,
        "variables": {
          "i": 5,
          "target": 15,
          "looking_at": 15,
          "checked": 6
        },
        "explanation": "Box 5 holds 15. That's what we wanted.",
        "stats": {
          "comparisons": 6,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c0-42",
            "value": 42
          },
          {
            "id": "c1-7",
            "value": 7
          },
          {
            "id": "c2-19",
            "value": 19
          },
          {
            "id": "c3-3",
            "value": 3
          },
          {
            "id": "c4-88",
            "value": 88
          },
          {
            "id": "c5-15",
            "value": 15
          },
          {
            "id": "c6-61",
            "value": 61
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 5
          }
        ],
        "highlights": [
          {
            "index": 5,
            "role": "found"
          }
        ],
        "codeLine": 4,
        "variables": {
          "result": 5,
          "checked": 6
        },
        "explanation": "Found 15 at index 5 after checking 6 boxes.",
        "stats": {
          "comparisons": 6,
          "swaps": 0
        }
      }
    ]
  }
}