{"id":1035,"date":"2026-10-30T12:16:03","date_gmt":"2026-10-30T12:16:03","guid":{"rendered":"https:\/\/blog.debuggersspace.com\/?p=1035"},"modified":"2026-10-30T14:12:02","modified_gmt":"2026-10-30T14:12:02","slug":"find-the-second-largest-number-in-an-array-in-one-pass","status":"publish","type":"post","link":"https:\/\/blog.debuggersspace.com\/index.php\/2026\/10\/30\/find-the-second-largest-number-in-an-array-in-one-pass\/","title":{"rendered":"Find the second largest number in an array in one pass."},"content":{"rendered":"<div class='booster-block booster-read-block'>\n                <div class=\"twp-read-time\">\n                \t<i class=\"booster-icon twp-clock\"><\/i> <span>Read Time:<\/span>1 Minute, 31 Second                <\/div>\n\n            <\/div>\n<p class=\"wp-block-paragraph\">Sorting and taking the second last element works, but costs O(n log n) and fails on duplicates. The interviewer wants one pass with two variables.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">One pass, two variables<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>static Integer secondLargest(int[] a) {\n    if (a == null || a.length &lt; 2) return null;\n    long first = Long.MIN_VALUE, second = Long.MIN_VALUE;\n    for (int x : a) {\n        if (x &gt; first) {\n            second = first;\n            first = x;\n        } else if (x &gt; second &amp;&amp; x &lt; first) {\n            second = x;\n        }\n    }\n    return second == Long.MIN_VALUE ? null : (int) second;\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Why it is written this way<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>When a new largest arrives, the old largest becomes second: that is the line <code>second = first<\/code>.<\/li>\n\n\n<li><code>x &lt; first<\/code> skips duplicates of the largest, so <code>[5, 5, 3]<\/code> gives 3, not 5.<\/li>\n\n\n<li>Starting from <code>Long.MIN_VALUE<\/code> instead of 0 handles all-negative arrays such as <code>[-4, -2, -9]<\/code>, which gives -4.<\/li>\n\n\n<li>Using <code>long<\/code> for the trackers means a real <code>Integer.MIN_VALUE<\/code> in the array is not confused with &#8220;not found&#8221;. Returning <code>null<\/code> covers arrays like <code>[7, 7, 7]<\/code> with no second distinct value.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Time O(n), space O(1).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Dry run on [12, 35, 1, 10, 34, 1]<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>x=12  first=12 second=-inf\nx=35  first=35 second=12\nx=1   no change\nx=10  no change\nx=34  second=34\nx=1   no change\nanswer: 34<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Follow-ups<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Find the kth largest: use a min-heap of size k (O(n log k)) or Quickselect (O(n) on average).<\/li>\n\n\n<li>Find the second smallest: flip the comparisons.<\/li>\n\n\n<li>What if the array is a stream you cannot store?<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><em>This answer was written by Debugger&#8217;s Space as practice material. It is asked in most Indian IT services interviews (TCS, Infosys, Wipro, HCLTech, Tech Mahindra, LTIMindtree, Accenture, Cognizant, Capgemini, IBM). See each company&#8217;s hiring steps and practice questions on <a href=\"https:\/\/www.debuggersspace.com\/companies\">Debugger&#8217;s Space<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Find the second largest element of an array in a single pass, handling duplicates, negative numbers and arrays with fewer than two distinct values.<\/p>\n","protected":false},"author":43,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","footnotes":""},"categories":[263],"tags":[234,289,236,270,235,275,231,237,229,264,233,228,232,230],"class_list":["post-1035","post","type-post","status-publish","format-standard","hentry","category-common-interview-questions","tag-accenture","tag-arrays","tag-capgemini","tag-coding-interview-questions","tag-cognizant","tag-freshers","tag-hcltech","tag-ibm","tag-infosys","tag-java-interview-questions","tag-ltimindtree","tag-tcs","tag-tech-mahindra","tag-wipro"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Himanshu Namdeo","author_link":"https:\/\/blog.debuggersspace.com\/author\/admin\/"},"uagb_comment_info":0,"uagb_excerpt":"Find the second largest element of an array in a single pass, handling duplicates, negative numbers and arrays with fewer than two distinct values.","_links":{"self":[{"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/posts\/1035","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/users\/43"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/comments?post=1035"}],"version-history":[{"count":1,"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/posts\/1035\/revisions"}],"predecessor-version":[{"id":1076,"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/posts\/1035\/revisions\/1076"}],"wp:attachment":[{"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/media?parent=1035"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/categories?post=1035"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/tags?post=1035"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}