Skip to content

Instantly share code, notes, and snippets.

@cjativa
Created October 31, 2024 14:47
Show Gist options
  • Save cjativa/89f18be6ec47a20545a8a4e1c692c291 to your computer and use it in GitHub Desktop.
Save cjativa/89f18be6ec47a20545a8a4e1c692c291 to your computer and use it in GitHub Desktop.
import expect from 'expect';
function mergeArraysNaive(
firstArray: Array<number>,
secondArray: Array<number>
) {
// Our approach will be a simplistic approach of
// 1. Identify the length of the first array, let's say N
// 2. From the second array, knowing anything past its own N-index
// is placeholder that can be thrown away, splice out those items
// 3. Merge those two portion of arrays
// 4. Sort the resulting merged array and return that
const endpoint = firstArray.length;
const portionOfSecondArray = secondArray.splice(0, endpoint);
// Merge the spliced off section of the second array onto the
// first array, and then just do a simple numeric sort
const mergedAndSortedArray = firstArray
.concat(portionOfSecondArray)
.sort((numberOne, numberTwo) => numberOne - numberTwo);
console.log(`
Merged and Sorted Array
${mergedAndSortedArray}
`);
return mergedAndSortedArray;
}
function mergeArraysOptimized(
firstArray: Array<number>,
secondArray: Array<number>
) {
// Our approach will be a simplistic approach of
// 1. Identify the length of the first array, let's say N
// 2. From the second array, knowing anything past its own N-index
// is placeholder that is not needed information
// 3. We'll iterate the first array and store each item from it into
// an index of the second array. The index to store into will be calculated
// as N + currentIndex and will look something like secondArray[N + currentIndex] = itemFromFirstArray
// 4. Sort the resulting merged array and return that
const endpoint = firstArray.length;
// Iterate the elements of the first array, gathering each of its items
// and storing the item at an index offset by its own length
// We're able to do this because we know anything in the second array past
// the index Nth is placeholders that are not needed, and we can essentially overwrite
for (
let currentIndex = 0;
currentIndex < firstArray.length;
currentIndex++
) {
const itemFromFirstArray = firstArray[currentIndex];
const indexToPlaceInto = currentIndex + endpoint;
// Store into this index in the second array
secondArray[indexToPlaceInto] = itemFromFirstArray;
}
// By this point, we've replaced each placeholder from the second array
// with each element from the first array. All we have to do now is sort the second array
const mergedAndSortedArray = secondArray.sort(
(numberOne, numberTwo) => numberOne - numberTwo
);
console.log(`
Merged and Sorted Array Optimized
${mergedAndSortedArray}
`);
return mergedAndSortedArray;
}
// The below outputs
// Merged and Sorted Array
// 1,3,4,6,7,8,9,10
const naiveArray = mergeArraysNaive([10, 9, 8, 7], [1, 3, 4, 6, 0, 0, 0, 0]);
// The below outputs
// Merged and Sorted Array Optimized
// 1,3,4,6,7,8,9,10
const optimizedArray = mergeArraysOptimized(
[10, 9, 8, 7],
[1, 3, 4, 6, 0, 0, 0, 0]
);
console.log(
`Both arrays are equal - ${naiveArray.sort().toString() === optimizedArray.sort().toString()}`
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment