Skip to content

Instantly share code, notes, and snippets.

@BenDMyers
Created May 16, 2022 14:58
Show Gist options
  • Save BenDMyers/77a4dab43d1599dae858ca6ea1b8f97f to your computer and use it in GitHub Desktop.
Save BenDMyers/77a4dab43d1599dae858ca6ea1b8f97f to your computer and use it in GitHub Desktop.
RWC: Maximized Array
/**
* 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