
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Kth Largest Element in an Array in Python
Suppose we have an unsorted array, we have to find the kth largest element from that array. So if the array is [3,2,1,5,6,4] and k = 2, then the result will be 5.
To solve this, we will follow these steps −
- We will sort the element,
- if the k is 1, then return last element, otherwise return array[n – k], where n is the size of the array.
Let us see the following implementation to get better understanding −
Example
class Solution(object): def findKthLargest(self, nums, k): nums.sort() if k ==1: return nums[-1] temp = 1 return nums[len(nums)-k] ob1 = Solution() print(ob1.findKthLargest([56,14,7,98,32,12,11,50,45,78,7,5,69], 5))
Input
[56,14,7,98,32,12,11,50,45,78,7,5,69] 5
Output
50
- Related Articles
- Kth Largest Element in an Array
- Kth Largest Element in a Stream in Python
- Python Program to find largest element in an array
- Python Program to find the largest element in an array
- C++ Program to Find kth Largest Element in a Sequence
- kth smallest/largest in a small range unsorted array in C++
- Program to find largest element in an array in C++
- Kth Smallest Element in a BST in Python
- Swap kth element of array - JavaScript
- Golang Program to Find the Largest Element in an Array
- Python – Find Kth Even Element
- Program to find kth missing positive number in an array in Python
- Kth Smallest Element in a Sorted Matrix in Python
- Kth odd number in an array in C++
- Detecting the largest element in an array of Numbers (nested) in JavaScript

Advertisements