Show
How to write a Python program to find the third-largest number in an array? How to print the third largest element in an array?Initially locate the biggest element, followed by the second largest element, and afterward barring them both locate the third-biggest component. Repeat the array iteration twice and mark the greatest and second biggest component and afterward barring them both locate the third greatest component, i.e the maximum element barring the greatest and second most greatest from the array. Input: array elements = [10,20,30,40,50] Algorithm to find the third largest element in an array:
Python program to print the third largest element in an array.import sys from array import * arr = array('i', []) n = int(input("enter number of elements")) for i in range(n): arr.append(int(input("enter the array elements"))) print("entered array is:") for i in range(len(arr)): print(arr[i]) def thirdLargest(arr, arr_size): if (arr_size < 3): print(" Please enter array size greater than 3 ") return first = arr[0] for i in range(1, arr_size): if (arr[i] > first): first = arr[i] second = -sys.maxsize for i in range(0, arr_size): if (arr[i] > second and arr[i] < first): second = arr[i] third = -sys.maxsize for i in range(0, arr_size): if (arr[i] > third and arr[i] < second): third = arr[i] print("The Third Largest", "element is", third) thirdLargest(arr, n)Output: Printing the third largest element in an arrayTo find the largest number in a list in Python:
Here is how it looks in code: heights = [100, 2, 300, 10, 11, 1000] largest_number = heights[0] for number in heights: if number > largest_number: largest_number = number print(largest_number)Output: This is the naive implementation of finding the largest number. But there are also some useful built-in mechanisms you can use. In this guide, you learn different ways to find the maximum value in a list in Python. The max() Function — Find the Largest Element of a ListIn Python, there is a built-in function max() you can use to find the largest number in a list. To use it, call the max() on a list of numbers. It then returns the greatest number in that list. Here is an example: heights = [100, 2, 300, 10, 11, 1000] max_height = max(heights) print(max_height)Output: 1000Alternative Approaches to Finding the Largest Number in a ListNow you know two straightforward ways to find the largest number in a list in Python. Let’s take a look at some more uncommon approaches. Reduce() FunctionYou can also use the functools reduce() function to find the largest number in a list. Before we do that, it is important to understand how the reduce() function works. The reduce function takes two parameters:
It then:
To learn more about the reduce() function, check this article. Anyway, let’s use the reduce() function to find the largest element in a list. Reduce() with the built-in max() FunctionHere is an example of how you can use reduce to find the largest number in a list: from functools import reduce heights = [100, 2, 300, 10, 11, 1000] max_height = reduce(max, heights) print(max_height)Output: 1000The reduce() function applies the max() function for each element as described in the previous chapter.
Let’s also see another, perhaps a bit more demonstrative example. Reduce() with a Custom Max FunctionAnother way you can use reduce() to find the largest number in a list is by implementing the max() function yourself. For example: from functools import reduce heights = [100, 2, 300, 10, 11, 1000] def my_max(x, y): if x < y: return y else: return x max_height = reduce(my_max, heights) print(max_height)Output Reduce() with a Lambda FunctionAnd the 3rd approach is to use reduce() with a lambda expression. This means you define the max function inline in the reduce() function call. For example: from functools import reduce heights = [100, 2, 300, 10, 11, 1000] max_height = reduce(lambda x, y: y if x < y else x, heights) print(max_height)Output: 1000The lambda x, y: y if x < y else x part does the same as the my_max() function in the previous example. Notice that the if-else statement is shortened to a one-liner expression. Find The Largest Number Using a Heap QueueThe built-in heapq module in Python comes in with an implementation of the priority queue algorithm. In short, a heap is a binary tree where each parent node has a value less than or equal to the value of its children. You can use the heapq.nlargest() function to figure out the largest number(s) in a list. For example: import heapq heights = [100, 2, 300, 10, 11, 1000] max_height = heapq.nlargest(1, heights)[0] print(max_height)Output: ConclusionToday you learned how to find the largest number in a list. First, you used the “brute-force” method to loop through the list while keeping track of the largest element. Then you saw how the built-in max() function does the job for you. Finally, you saw uncommon alternatives, such as using reduce() with lambdas or a heap queue. Each of these approaches gets the job done for you. Feel free to pick the approach that is best for your situation. If I had to find the largest element, I would pick the most readable approach, that is, the built-in max() function. Thanks for reading. I hope you find it useful. Happy coding! Further ReadingLambdas in Python |