Merge 2 sorted list

i was doing merge sorted list on leetcode bc i wanna see if i could bc i never did leetcode and did this one but im confused bc to me how i solved it makes sense but it doesnt work

function merge(list1, list2) {
    const newlist =[];
    for(let i = 0; i < list1.length; i++){
        for(let j =0; j <list2.length; j++){
            if(list1[i] < list2[j]){
 newlist.push(list1[i]);
            }else if(list1[i] > list2[j]){
                newlist.push(list2[j]);
            }else if(list1[i] === list2[j]){
                newlist.push(list1[i]);
                newlist.push(list2[j])
            }
        }
    }
    return newlist;
};
console.log(merge([1,2,4],[3,4]))

it doesn’t make sense, having two nested loops mean you push list1.length * list2.length items instead of list1.length + list2.length

Is that y * is making me return all the numbers twice? Bc it does sort thing correctly but return all numbers twice

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.