{"id":347,"date":"2025-06-22T14:03:45","date_gmt":"2025-06-22T08:33:45","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=347"},"modified":"2025-07-07T20:47:12","modified_gmt":"2025-07-07T15:17:12","slug":"linear-search-in-python","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/linear-search-in-python\/","title":{"rendered":"Linear Search | Explained in Python"},"content":{"rendered":"\n<p>Given an array,&nbsp;<strong>arr<\/strong>&nbsp;of n integers, and an integer element&nbsp;<strong>x<\/strong>, find whether element&nbsp;<strong>x<\/strong>&nbsp;is present in the array. Return the index of the first occurrence of&nbsp;<strong>x<\/strong>&nbsp;in the array, or -1 if it doesn&#8217;t exist.<\/p>\n\n\n\n<p>Here&#8217;s the [<strong><a href=\"https:\/\/www.geeksforgeeks.org\/problems\/search-an-element-in-an-array-1587115621\/1\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">Problem Link<\/span><\/mark><\/a><\/strong>] to begin with.<\/p>\n\n\n<div style=\"max-width: -moz-fit-content; \" class=\"wp-block-ub-table-of-contents-block ub_table-of-contents ub_table-of-contents-collapsed\" id=\"ub_table-of-contents-186be54e-832d-4aeb-8c4d-cb3713d585c2\" data-linktodivider=\"false\" data-showtext=\"show\" data-hidetext=\"hide\" data-scrolltype=\"auto\" data-enablesmoothscroll=\"true\" data-initiallyhideonmobile=\"false\" data-initiallyshow=\"false\"><div class=\"ub_table-of-contents-header-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-header\" style=\"text-align: left; \">\n\t\t\t\t<div class=\"ub_table-of-contents-title\">Contents:<\/div>\n\t\t\t\t<div class=\"ub_table-of-contents-header-toggle\">\n\t\t\t<div class=\"ub_table-of-contents-toggle\" style=\"\">\n\t\t\t\u00a0[<a class=\"ub_table-of-contents-toggle-link\" href=\"#\" style=\"\">show<\/a>]\n\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t<\/div>\n\t\t<\/div><div class=\"ub_table-of-contents-extra-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-container ub_table-of-contents-1-column ub-hide\">\n\t\t\t\t<ul style=\"\"><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/linear-search-in-python\/#0-1-what-does-the-question-says-\" style=\"\">1. What does the question says?<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/linear-search-in-python\/#1-2-how-do-we-approach-the-problem-\" style=\"\">2. How do we approach the problem?<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/linear-search-in-python\/#2-3-implementation-\" style=\"\">3. Implementation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/linear-search-in-python\/#3-4-step-by-step-explanation-\" style=\"\">4. Step-by-Step Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/linear-search-in-python\/#4-5-dry-run-\" style=\"\">5. Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/linear-search-in-python\/#5-6-potential-edge-cases-\" style=\"\">6. Potential Edge Cases<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/linear-search-in-python\/#6-7-time-and-space-complexity-\" style=\"\">7. Time and Space Complexity<\/a><\/li><\/ul>\n\t\t\t<\/div>\n\t\t<\/div><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"0-1-what-does-the-question-says-\"><strong>1. What does the question says?<\/strong><\/h3>\n\n\n\n<p>This question is asking us to <strong>find the position<\/strong> (0-based index) of a specified value in an array. Specifically:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>We are provided:\n<ul class=\"wp-block-list\">\n<li>An integer <strong>n<\/strong> denoting the size of the array.<\/li>\n\n\n\n<li>An integer <strong>num<\/strong> which is the <strong>target element<\/strong> to be searched.<\/li>\n\n\n\n<li>An array <strong>arr<\/strong> of length <strong>n<\/strong>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Our task is to determine whether <em>num<\/em> is <strong>present<\/strong> in the array <em>arr<\/em>.\n<ul class=\"wp-block-list\">\n<li>If it is present, <strong>return the index<\/strong> (0-based) of its <strong>first<\/strong> occurrence.<\/li>\n\n\n\n<li>If it is <strong>not<\/strong> found in the array, <strong>return \u22121<\/strong>.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p>Essentially, the question is about scanning the array and identifying if the target value num appears. If so, we should report the position where it appears <em>for the first time<\/em>. If it never appears, we return \u22121.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-2-how-do-we-approach-the-problem-\"><strong>2. How do we approach the problem?<\/strong><\/h3>\n\n\n\n<p>To solve this, we typically employ a method known as <strong>linear search<\/strong>:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Definition of Linear Search<\/strong>\n<ul class=\"wp-block-list\">\n<li>Linear search involves traversing the array from the <strong>start<\/strong> to the <strong>end<\/strong> and comparing each element to the target, num.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Why Linear Search Works Here<\/strong>\n<ul class=\"wp-block-list\">\n<li>We do not have any conditions like a <em>sorted array<\/em> that might allow more specialized techniques (e.g., binary search).<\/li>\n\n\n\n<li>Linear search is <strong>simple<\/strong> and ensures that every element is checked, so we <strong>cannot miss<\/strong> the target if it is indeed present.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Core Idea<\/strong>\n<ul class=\"wp-block-list\">\n<li>Start at index 0.<\/li>\n\n\n\n<li>Compare arr[i] with num.<\/li>\n\n\n\n<li>If they match, <strong>return <\/strong><strong>i<\/strong> immediately.<\/li>\n\n\n\n<li>If not, move to the <strong>next index<\/strong> and repeat.<\/li>\n\n\n\n<li>If we reach the end of the array without a match, return \u22121.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p>Thus, by carefully checking each element in order, we can reliably determine if num exists in arr, and if so, at which index we first encounter it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-3-implementation-\"><strong>3. Implementation<\/strong><\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"def linearSearch(n: int, num: int, arr: [int]) -&gt; int:\n    for i in range(0, len(arr)):\n        if arr[i] == num:\n            return i\n    return -1\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">linearSearch<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">n<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">num<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">arr<\/span><span style=\"color: #D4D4D4\">: [<\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">]) -&gt; <\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> i <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(arr)):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> arr[i] == num:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> i<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> -<\/span><span style=\"color: #B5CEA8\">1<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-4-step-by-step-explanation-\"><strong>4. Step-by-Step Explanation<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Loop Initialization<\/strong>: Start from index 0 and go up to len(arr) &#8211; 1.<\/li>\n\n\n\n<li><strong>Comparison<\/strong>: At each index i, check if arr[i] equals num.<\/li>\n\n\n\n<li><strong>Early Return<\/strong>: If they match, return i.<\/li>\n\n\n\n<li><strong>Conclusion<\/strong>: If no match is found by the end, return -1.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-5-dry-run-\"><strong>5. Dry Run<\/strong><\/h3>\n\n\n\n<p>Let&#8217;s go through the code step by step, using images to illustrate the detailed dry run process.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/image-1024x576.png\" alt=\"\" class=\"wp-image-349\" srcset=\"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/image-1024x576.png 1024w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/image-300x169.png 300w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/image-768x432.png 768w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/image-1536x864.png 1536w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/image-150x84.png 150w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/image-450x253.png 450w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/image-1200x675.png 1200w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/image.png 1600w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-6-potential-edge-cases-\"><strong>6. Potential Edge Cases<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Array of Size 1<\/strong>:\n<ul class=\"wp-block-list\">\n<li>If arr = [7] and num = 7, returns 0. Otherwise, -1.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Value Not Present<\/strong>:\n<ul class=\"wp-block-list\">\n<li>If arr = [1, 2, 3] and num = 4, returns -1.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Multiple Occurrences<\/strong>:\n<ul class=\"wp-block-list\">\n<li>If arr = [2, 7, 7, 9] and num = 7, returns the first index (1).<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Value at the Last Position<\/strong>:\n<ul class=\"wp-block-list\">\n<li>If arr = [2, 3, 4, 5, 7] and num = 7, returns 4.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Empty Array<\/strong> (if allowed):\n<ul class=\"wp-block-list\">\n<li>If n = 0, returns -1 immediately (though constraints may not allow this case).<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-7-time-and-space-complexity-\"><strong>7. Time and Space Complexity<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity<\/strong>: <strong>O(n)<\/strong> in the worst case, because we may scan each element once.<\/li>\n\n\n\n<li><strong>Space Complexity<\/strong>: <strong>O(1)<\/strong>, as we only use a few auxiliary variables.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/www.codeanddebug.in\/course\/zero-to-hero-python-dsa\">Join our Advance DSA COURSE<\/a><\/div>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><em>For any changes to the article, kindly email at <a href=\"mailto:code@codeanddebug.in\">code@codeanddebug.in<\/a> or contact us at <a href=\"tel:+91-9712928220\" target=\"_blank\" rel=\"noreferrer noopener\">+91-9712928220.<\/a><\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given an array,&nbsp;arr&nbsp;of n integers, and an integer element&nbsp;x, find whether element&nbsp;x&nbsp;is present in the array. Return the index of the first occurrence of&nbsp;x&nbsp;in the array, or -1 if it doesn&#8217;t exist. Here&#8217;s the [Problem Link] to begin with. 1. What does the question says? This question is asking us to find the position (0-based<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[],"class_list":{"0":"post-347","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-data-structures-and-algorithm","7":"category-beginner"},"featured_image_src":null,"author_info":{"display_name":"codeanddebug","author_link":"https:\/\/codeanddebug.in\/blog\/author\/codeanddebug\/"},"_links":{"self":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/347","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/comments?post=347"}],"version-history":[{"count":3,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/347\/revisions"}],"predecessor-version":[{"id":556,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/347\/revisions\/556"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=347"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=347"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=347"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}