Skip to content

Instantly share code, notes, and snippets.

@funyin
Created October 17, 2022 00:00
Show Gist options
  • Save funyin/59fc9237635121d3f2052c40e3c6e504 to your computer and use it in GitHub Desktop.
Save funyin/59fc9237635121d3f2052c40e3c6e504 to your computer and use it in GitHub Desktop.
Kth largest element
/**
* Find Kth Largest element in an integer array
*/
fun main(){
val array = intArrayOf(1,1,1,2,1,4,1,7,8,4,2,5,9,9,4,7,2,0,4,6,1)
print(array.kTthLargestElement(3))
}
fun IntArray.kTthLargestElement(k: Int): Int {
sort()
return this[size - k]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment