Created
July 30, 2019 03:12
-
-
Save wataruoguchi/ddadd54224d0667db8ff34790d48be54 to your computer and use it in GitHub Desktop.
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
// https://www.geeksforgeeks.org/k-largestor-smallest-elements-in-an-array/ | |
// https://practice.geeksforgeeks.org/problems/k-largest-elements/0 | |
function kLargest(arr, n, k) { | |
return arr.sort((a, b) => b - a).splice(0, k); | |
} | |
const arrEx1 = [12,5,787,1,23]; | |
const resEx1 = kLargest(arrEx1, arrEx1.length, 2); | |
console.log(resEx1.join(',') === '787,23'); | |
const arrEx2 = [1,23,12,9,30,2,50]; | |
const resEx2 = kLargest(arrEx2, arrEx2.length, 3); | |
console.log(resEx2.join(',') === '50,30,23'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment