1. 2015
    Nov
    5

    Merging 2 JavaScript arrays from a set size

    Posted By



    The inspiration for this solution came from an interview question to do a function that allows you insert array B into array A by a specified size N and sort the combined result. Surprisingly this took me a bit of a while to figure out, I wasn’t able to benefit from the solution.

    Since I am merging 2 arrays for this solution I decided not to take into account making the items in the resulting array unique because it was not required. So if the same value exists in both arrays, the same value would likely repeat twice in the combined array.

    
    function mergeArray(n, a, b) {
        return a.sort().concat(b.sort()).slice(0,(n*2)).sort();
    }
    
    mergeArray(3, [2,3,7], [1,4,5]); 
    

  2. About Emeka Echeruo

    Emeka Echeruo

    I love sports, football which I refuse to call soccer, and the outdoor especially walks in park. Software development is my passion, there is a beauty in creating something out of nothing but algebra that ends up becomes a part of a persons daily life. I love kids, dogs, nightlife and art because it finds you and moves you emotionally!

  3. Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.