Created
October 17, 2022 00:00
-
-
Save funyin/59fc9237635121d3f2052c40e3c6e504 to your computer and use it in GitHub Desktop.
Kth largest element
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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