{"id":40,"date":"2025-05-08T19:47:02","date_gmt":"2025-05-08T14:17:02","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=40"},"modified":"2025-05-08T20:04:29","modified_gmt":"2025-05-08T14:34:29","slug":"find-the-largest-element-in-an-array","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/find-the-largest-element-in-an-array\/","title":{"rendered":"Find the Largest Element in an Array"},"content":{"rendered":"\n<p>Hi everyone, in this article we&#8217;ll guide you through the Python program to find the largest element in an Array without Sorting [<strong><a href=\"https:\/\/www.geeksforgeeks.org\/problems\/largest-element-in-array4009\/0\" 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>]. Comprehensively with different examples and dry run. So let&#8217;s begin.<\/p>\n\n\n<div style=\"max-width: -moz-fit-content; \" class=\"wp-block-ub-table-of-contents-block ub_table-of-contents\" id=\"ub_table-of-contents-45f4d3d6-fc9f-4736-84b8-96c2adf5eea9\" data-linktodivider=\"false\" data-showtext=\"show\" data-hidetext=\"hide\" data-scrolltype=\"auto\" data-enablesmoothscroll=\"true\" data-initiallyhideonmobile=\"false\" data-initiallyshow=\"true\"><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\">Content<\/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=\"\">hide<\/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 \">\n\t\t\t\t<ul style=\"\"><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-the-largest-element-in-an-array\/#0-examples-to-understand-the-program\" style=\"\">Examples to understand the program<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-the-largest-element-in-an-array\/#1-1-brute-force-solution-with-sorting\" style=\"\">1. Brute Force Solution (With Sorting)<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-the-largest-element-in-an-array\/#2-intuition-and-approach\" style=\"\">Intuition and Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-the-largest-element-in-an-array\/#3-code\" style=\"\">Code<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-the-largest-element-in-an-array\/#4-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-the-largest-element-in-an-array\/#5-dry-run\" style=\"\">Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-the-largest-element-in-an-array\/#6-edge-cases-to-consider\" style=\"\">Edge cases to consider<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-the-largest-element-in-an-array\/#7-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-the-largest-element-in-an-array\/#8-2-optimal-solution-single-pass-solution\" style=\"\">2. Optimal Solution (Single Pass Solution)<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-the-largest-element-in-an-array\/#9-intuition-and-approach\" style=\"\">Intuition and Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-the-largest-element-in-an-array\/#10-code\" style=\"\">Code<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-the-largest-element-in-an-array\/#11-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-the-largest-element-in-an-array\/#12-dry-run\" style=\"\">Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-the-largest-element-in-an-array\/#13-edge-cases-to-consider\" style=\"\">Edge cases to consider<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-the-largest-element-in-an-array\/#14-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><\/ul><\/li><\/ul>\n\t\t\t<\/div>\n\t\t<\/div><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"0-examples-to-understand-the-program\">Examples to understand the program<\/h2>\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:#282A36\"><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=\"Example 1\nInput: arr = [1, 8, 7, 56, 90]\nOutput: 90\nExplanation: The largest element of the given array is 90.\n\nInput: arr = [5, 5, 5, 5]\nOutput: 5\nExplanation: The largest element of the given array is 5.\n\nInput: arr = [10]\nOutput: 10\nExplanation: There is only one element which is the largest.\" style=\"color:#F8F8F2;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 dracula\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F8F8F2\">Example <\/span><span style=\"color: #BD93F9\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F8F8F2\">Input: arr <\/span><span style=\"color: #FF79C6\">=<\/span><span style=\"color: #F8F8F2\"> [<\/span><span style=\"color: #BD93F9\">1<\/span><span style=\"color: #F8F8F2\">, <\/span><span style=\"color: #BD93F9\">8<\/span><span style=\"color: #F8F8F2\">, <\/span><span style=\"color: #BD93F9\">7<\/span><span style=\"color: #F8F8F2\">, <\/span><span style=\"color: #BD93F9\">56<\/span><span style=\"color: #F8F8F2\">, <\/span><span style=\"color: #BD93F9\">90<\/span><span style=\"color: #F8F8F2\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F8F8F2\">Output: <\/span><span style=\"color: #BD93F9\">90<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F8F8F2\">Explanation: The largest element of the given array <\/span><span style=\"color: #FF79C6\">is<\/span><span style=\"color: #F8F8F2\"> <\/span><span style=\"color: #BD93F9\">90<\/span><span style=\"color: #F8F8F2\">.<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F8F8F2\">Input: arr <\/span><span style=\"color: #FF79C6\">=<\/span><span style=\"color: #F8F8F2\"> [<\/span><span style=\"color: #BD93F9\">5<\/span><span style=\"color: #F8F8F2\">, <\/span><span style=\"color: #BD93F9\">5<\/span><span style=\"color: #F8F8F2\">, <\/span><span style=\"color: #BD93F9\">5<\/span><span style=\"color: #F8F8F2\">, <\/span><span style=\"color: #BD93F9\">5<\/span><span style=\"color: #F8F8F2\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F8F8F2\">Output: <\/span><span style=\"color: #BD93F9\">5<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F8F8F2\">Explanation: The largest element of the given array <\/span><span style=\"color: #FF79C6\">is<\/span><span style=\"color: #F8F8F2\"> <\/span><span style=\"color: #BD93F9\">5<\/span><span style=\"color: #F8F8F2\">.<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F8F8F2\">Input: arr <\/span><span style=\"color: #FF79C6\">=<\/span><span style=\"color: #F8F8F2\"> [<\/span><span style=\"color: #BD93F9\">10<\/span><span style=\"color: #F8F8F2\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F8F8F2\">Output: <\/span><span style=\"color: #BD93F9\">10<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F8F8F2\">Explanation: There <\/span><span style=\"color: #FF79C6\">is<\/span><span style=\"color: #F8F8F2\"> only one element which <\/span><span style=\"color: #FF79C6\">is<\/span><span style=\"color: #F8F8F2\"> the largest.<\/span><\/span><\/code><\/pre><\/div>\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:#282A36\"><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=\"Constraints:\n1 &lt;= arr.size()&lt;= 10^6\n0 &lt;= arr[i] &lt;= 10^6\" style=\"color:#F8F8F2;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 dracula\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F8F8F2\">Constraints:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #BD93F9\">1<\/span><span style=\"color: #F8F8F2\"> <\/span><span style=\"color: #FF79C6\">&lt;=<\/span><span style=\"color: #F8F8F2\"> arr.size()<\/span><span style=\"color: #FF79C6\">&lt;=<\/span><span style=\"color: #F8F8F2\"> <\/span><span style=\"color: #BD93F9\">10<\/span><span style=\"color: #FF79C6\">^<\/span><span style=\"color: #BD93F9\">6<\/span><\/span>\n<span class=\"line\"><span style=\"color: #BD93F9\">0<\/span><span style=\"color: #F8F8F2\"> <\/span><span style=\"color: #FF79C6\">&lt;=<\/span><span style=\"color: #F8F8F2\"> arr[i] <\/span><span style=\"color: #FF79C6\">&lt;=<\/span><span style=\"color: #F8F8F2\"> <\/span><span style=\"color: #BD93F9\">10<\/span><span style=\"color: #FF79C6\">^<\/span><span style=\"color: #BD93F9\">6<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><em>Also read about the Python Program to <strong><a href=\"https:\/\/codeanddebug.in\/blog\/remove-duplicates-from-sorted-array-leetcode-26\/\" 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;\">Remove duplicates from Sorted array<\/span><\/mark><\/a><\/strong><\/em>.<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"1-1-brute-force-solution-with-sorting\">1. Brute Force Solution (With Sorting)<\/h2>\n\n\n\n<p>This approach uses sorting to solve the problem. While it might not be the most efficient solution, it still gets the job done.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-intuition-and-approach\">Intuition and Approach<\/h3>\n\n\n\n<p><strong>Intuition:<\/strong><\/p>\n\n\n\n<p>Sorting the array arranges the elements in ascending order, so the largest element will be at the last position after sorting.<\/p>\n\n\n\n<p><strong>Approach:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Sort the Array:<\/strong> Sort the array in non-decreasing order. This makes sure that the largest element is placed at the last index of the array.<\/li>\n\n\n\n<li><strong>Return the Largest Element:<\/strong> After sorting, the last element in the array is the largest, so return this element.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-code\">Code<\/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:#282A36\"><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 largestElement(arr: [], n: int) -&gt; int:\n    # Sort the elements first\n    arr.sort()\n\n    # Return the largest from the end\n    return arr[-1]\" style=\"color:#F8F8F2;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 dracula\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #FF79C6\">def<\/span><span style=\"color: #F8F8F2\"> <\/span><span style=\"color: #50FA7B\">largestElement<\/span><span style=\"color: #F8F8F2\">(<\/span><span style=\"color: #FFB86C; font-style: italic\">arr<\/span><span style=\"color: #FF79C6\">:<\/span><span style=\"color: #F8F8F2\"> [], <\/span><span style=\"color: #FFB86C; font-style: italic\">n<\/span><span style=\"color: #FF79C6\">:<\/span><span style=\"color: #F8F8F2\"> <\/span><span style=\"color: #8BE9FD; font-style: italic\">int<\/span><span style=\"color: #F8F8F2\">) <\/span><span style=\"color: #FF79C6\">-&gt;<\/span><span style=\"color: #F8F8F2\"> <\/span><span style=\"color: #8BE9FD; font-style: italic\">int<\/span><span style=\"color: #F8F8F2\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F8F8F2\">    <\/span><span style=\"color: #6272A4\"># Sort the elements first<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F8F8F2\">    arr.sort()<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F8F8F2\">    <\/span><span style=\"color: #6272A4\"># Return the largest from the end<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F8F8F2\">    <\/span><span style=\"color: #FF79C6\">return<\/span><span style=\"color: #F8F8F2\"> arr[<\/span><span style=\"color: #FF79C6\">-<\/span><span style=\"color: #BD93F9\">1<\/span><span style=\"color: #F8F8F2\">]<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-code-explanation\">Code Explanation<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Sort the Array:<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>arr.sort():<\/strong> This line sorts the array <strong>arr<\/strong> in ascending order. Sorting rearranges the elements such that the smallest element is at the beginning and the largest element is at the end of the array.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Return the Largest Element:<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>return arr[-1]:<\/strong> After sorting, the last element (<strong>arr[-1]<\/strong>) is the largest element in the array. This value is returned as the result.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-dry-run\">Dry Run<\/h3>\n\n\n\n<p>Let&#8217;s walk through a step-by-step execution with a sample input:<\/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\/05\/largest-element-in-an-array-brute-force-solution-1024x576.png\" alt=\"\" class=\"wp-image-41\" srcset=\"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/largest-element-in-an-array-brute-force-solution-1024x576.png 1024w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/largest-element-in-an-array-brute-force-solution-300x169.png 300w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/largest-element-in-an-array-brute-force-solution-768x432.png 768w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/largest-element-in-an-array-brute-force-solution-1536x864.png 1536w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/largest-element-in-an-array-brute-force-solution-150x84.png 150w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/largest-element-in-an-array-brute-force-solution-450x253.png 450w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/largest-element-in-an-array-brute-force-solution-1200x675.png 1200w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/largest-element-in-an-array-brute-force-solution.png 1920w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-edge-cases-to-consider\">Edge cases to consider<\/h3>\n\n\n\n<ol id=\"block-b60a7ec0-385e-40e7-b71d-a83313a51bf0\" class=\"wp-block-list\">\n<li><strong>Single Element Array:<\/strong> If the array contains only one element, that element is the largest by default.<\/li>\n\n\n\n<li><strong>All Elements Are Identical:<\/strong> If all elements in the array are the same, that element is the largest.<\/li>\n\n\n\n<li><strong>Unsorted Input:<\/strong> The input array is not necessarily sorted, so sorting make sure we get the solution.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"7-time-and-space-complexity\">Time and Space Complexity<\/h3>\n\n\n\n<ol id=\"block-4597edcd-d063-4853-999b-b2b4df19b0d1\" class=\"wp-block-list\">\n<li><strong>Time Complexity<\/strong>\n<ul class=\"wp-block-list\">\n<li>The time complexity is <strong>O(nlog\u2061n)<\/strong>, where <strong>n<\/strong> is the length of the array <strong>arr<\/strong>. This is due to the time required to sort the array.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>\n<ul class=\"wp-block-list\">\n<li>The space complexity is <strong>O(1)<\/strong> if we ignore the space required by the sorting algorithm (which may vary depending on the implementation). Our does not use any extra space other than input and output.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\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\" target=\"_blank\" rel=\"noreferrer noopener\">Join our advance python with dsa course<\/a><\/div>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"8-2-optimal-solution-single-pass-solution\">2. Optimal Solution (Single Pass Solution)<\/h2>\n\n\n\n<p>This approach uses iteration and maintaing the largest number in a variable and returning it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"9-intuition-and-approach\">Intuition and Approach<\/h3>\n\n\n\n<p><strong>Intuition:<\/strong><\/p>\n\n\n\n<p>The idea is to initialize a variable with the first element of the array and then iterate through the array to compare each element with this variable. If any element is larger than the current value, update the variable to this new value. After completing the iteration, the variable will hold the largest element.<\/p>\n\n\n\n<p><strong>Approach:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Initialize the Maximum Value:<\/strong> Start by assuming the first element is the largest.<\/li>\n\n\n\n<li><strong>Iterate Through the Array:<\/strong> Compare each element with the current maximum value. If a larger element is found, update the maximum value.<\/li>\n\n\n\n<li><strong>Return the Largest Element:<\/strong> After completing the iteration, return the value stored in the maximum variable.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"10-code\">Code<\/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:#282A36\"><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 largestElement(arr: [], n: int) -&gt; int:\n    max_num = arr[0]\n    \n    for num in arr:\n        if num &gt; max_num:\n            max_num = num\n    \n    return max_num\" style=\"color:#F8F8F2;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 dracula\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #FF79C6\">def<\/span><span style=\"color: #F8F8F2\"> <\/span><span style=\"color: #50FA7B\">largestElement<\/span><span style=\"color: #F8F8F2\">(<\/span><span style=\"color: #FFB86C; font-style: italic\">arr<\/span><span style=\"color: #FF79C6\">:<\/span><span style=\"color: #F8F8F2\"> [], <\/span><span style=\"color: #FFB86C; font-style: italic\">n<\/span><span style=\"color: #FF79C6\">:<\/span><span style=\"color: #F8F8F2\"> <\/span><span style=\"color: #8BE9FD; font-style: italic\">int<\/span><span style=\"color: #F8F8F2\">) <\/span><span style=\"color: #FF79C6\">-&gt;<\/span><span style=\"color: #F8F8F2\"> <\/span><span style=\"color: #8BE9FD; font-style: italic\">int<\/span><span style=\"color: #F8F8F2\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F8F8F2\">    max_num <\/span><span style=\"color: #FF79C6\">=<\/span><span style=\"color: #F8F8F2\"> arr[<\/span><span style=\"color: #BD93F9\">0<\/span><span style=\"color: #F8F8F2\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F8F8F2\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #F8F8F2\">    <\/span><span style=\"color: #FF79C6\">for<\/span><span style=\"color: #F8F8F2\"> num <\/span><span style=\"color: #FF79C6\">in<\/span><span style=\"color: #F8F8F2\"> arr:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F8F8F2\">        <\/span><span style=\"color: #FF79C6\">if<\/span><span style=\"color: #F8F8F2\"> num <\/span><span style=\"color: #FF79C6\">&gt;<\/span><span style=\"color: #F8F8F2\"> max_num:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F8F8F2\">            max_num <\/span><span style=\"color: #FF79C6\">=<\/span><span style=\"color: #F8F8F2\"> num<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F8F8F2\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #F8F8F2\">    <\/span><span style=\"color: #FF79C6\">return<\/span><span style=\"color: #F8F8F2\"> max_num<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"11-code-explanation\">Code Explanation<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Initialize the Maximum Value:<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>max_num = arr[0]:<\/strong> This line initializes the <strong>max_num<\/strong> variable with the first element of the array. This variable will store the largest element found during the iteration.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Iterate Through the Array:<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>for num in arr:<\/strong> This loop goes through each element in the array.<\/li>\n\n\n\n<li><strong>if num &gt; max_num:<\/strong> If the current element (<strong>num<\/strong>) is greater than <strong>max_num<\/strong>, then update <strong>max_num <\/strong>to the current element.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Return the Largest Element:<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>return max_num:<\/strong> After iterating through the entire array, <strong>max_num<\/strong> will contain the largest element, which is then returned.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"12-dry-run\">Dry Run<\/h3>\n\n\n\n<p>Let&#8217;s walk through a step-by-step execution with a sample input:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/largest-element-in-an-array-optimal-solution-step1-1024x576.png\" alt=\"\" class=\"wp-image-42\" srcset=\"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/largest-element-in-an-array-optimal-solution-step1-1024x576.png 1024w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/largest-element-in-an-array-optimal-solution-step1-300x169.png 300w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/largest-element-in-an-array-optimal-solution-step1-768x432.png 768w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/largest-element-in-an-array-optimal-solution-step1-1536x864.png 1536w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/largest-element-in-an-array-optimal-solution-step1-150x84.png 150w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/largest-element-in-an-array-optimal-solution-step1-450x253.png 450w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/largest-element-in-an-array-optimal-solution-step1-1200x675.png 1200w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/largest-element-in-an-array-optimal-solution-step1.png 1920w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/largest-element-in-an-array-optimal-solution-step2-1024x576.png\" alt=\"\" class=\"wp-image-43\" srcset=\"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/largest-element-in-an-array-optimal-solution-step2-1024x576.png 1024w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/largest-element-in-an-array-optimal-solution-step2-300x169.png 300w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/largest-element-in-an-array-optimal-solution-step2-768x432.png 768w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/largest-element-in-an-array-optimal-solution-step2-1536x864.png 1536w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/largest-element-in-an-array-optimal-solution-step2-150x84.png 150w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/largest-element-in-an-array-optimal-solution-step2-450x253.png 450w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/largest-element-in-an-array-optimal-solution-step2-1200x675.png 1200w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/largest-element-in-an-array-optimal-solution-step2.png 1920w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"316\" src=\"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/largest-element-in-an-array-optimal-solution-step3-1024x316.png\" alt=\"\" class=\"wp-image-44\" srcset=\"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/largest-element-in-an-array-optimal-solution-step3-1024x316.png 1024w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/largest-element-in-an-array-optimal-solution-step3-300x93.png 300w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/largest-element-in-an-array-optimal-solution-step3-768x237.png 768w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/largest-element-in-an-array-optimal-solution-step3-1536x474.png 1536w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/largest-element-in-an-array-optimal-solution-step3-150x46.png 150w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/largest-element-in-an-array-optimal-solution-step3-450x139.png 450w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/largest-element-in-an-array-optimal-solution-step3-1200x371.png 1200w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/largest-element-in-an-array-optimal-solution-step3.png 1920w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"13-edge-cases-to-consider\">Edge cases to consider<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Single Element Array: <\/strong>If the array contains only one element, that element is the <strong>largest<\/strong> by default.<\/li>\n\n\n\n<li><strong>All Elements Are Identical:<\/strong> If all elements in the array are the same, the function will correctly return that element.<\/li>\n\n\n\n<li><strong>Negative Numbers:<\/strong> Our code should correctly handle arrays with negative numbers, where the largest number is the one closest to zero.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"14-time-and-space-complexity\">Time and Space Complexity<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong>\n<ul class=\"wp-block-list\">\n<li>The time complexity is <strong>O(n)<\/strong>, where <strong>n<\/strong> is the length of the array <strong>arr<\/strong>. This is because our code needs to iterate through all elements in the array.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>The space complexity is <strong>O(1)<\/strong> because the function only uses a constant amount of extra space (<strong>for the max_num variable<\/strong>) regardless of the size of the input array.<\/li>\n<\/ol>\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:\/\/codeanddebug.in\/course\/master-dsa-with-leetcode\" target=\"_blank\" rel=\"noreferrer noopener\">JOIN OUR FREE 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 document, kindly email at <a href=\"mailto:code@codeanddebug.in\">code@codeanddebug.in<\/a> or contact us at <a href=\"tel:+91-9712928220\">+91-9712928220<\/a>.<\/em><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi everyone, in this article we&#8217;ll guide you through the Python program to find the largest element in an Array without Sorting [Problem Link]. Comprehensively with different examples and dry run. So let&#8217;s begin. Examples to understand the program Also read about the Python Program to Remove duplicates from Sorted array. 1. Brute Force Solution<\/p>\n","protected":false},"author":1,"featured_media":45,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[7,8],"class_list":{"0":"post-40","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-beginner","9":"tag-array","10":"tag-easy"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/largest-element-in-an-array-featured-image.jpg","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\/40","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=40"}],"version-history":[{"count":2,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/40\/revisions"}],"predecessor-version":[{"id":55,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/40\/revisions\/55"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/45"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=40"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=40"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=40"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}