Created
May 16, 2022 14:58
-
-
Save BenDMyers/77a4dab43d1599dae858ca6ea1b8f97f to your computer and use it in GitHub Desktop.
RWC: Maximized Array
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
/** | |
* Comparison function for two integers which will sort the bigger one first | |
* @param {number} int1 | |
* @param {number} int2 | |
*/ | |
function bySize(int1, int2) { | |
return int2 - int1; | |
} | |
/** | |
* | |
* @param {number[]} arr1 first list of integers | |
* @param {number[]} arr2 second list of integers | |
*/ | |
function maximizedArray(arr1, arr2) { | |
const uniqueIntegers = new Set([...arr1, ...arr2]); | |
return Array | |
.from(uniqueIntegers) | |
.sort(bySize) | |
.slice(0, arr1.length); | |
} | |
console.log( | |
maximizedArray([7, 4, 10, 0, 1], [9, 7, 2, 3, 6]) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment