Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR TYPESCRIPT

typescript / javascript merge sorted arrays

function MergeSorted(arr1: number[], arr2: number[])
: number[]{
    let x: number = 0;
    let y: number = 0;

    let Arr1Index: number = arr1[x];
    let Arr2Index: number = arr2[y];

    let MergedArray: number[] = [];

    while(Arr1Index || Arr2Index){
        if(!Arr2Index || (Arr1Index < Arr2Index)){
            MergedArray.push(Arr1Index);
            Arr1Index = arr1[x];
            x++;
        }else{
            MergedArray.push(Arr2Index);
            Arr2Index = arr2[y];
            y++;
        }
    }

    return MergedArray;
}
 
PREVIOUS NEXT
Tagged: #typescript #javascript #merge #sorted #arrays
ADD COMMENT
Topic
Name
7+8 =