Thread: can i merge 2 arrays with 1 loop?

  1. #1
    Registered User
    Join Date
    Jul 2018
    Posts
    10

    can i merge 2 arrays with 1 loop?

    hey .. i was told to make an void mergeArrays(int[] ar1 , int[] ar2);
    i made it in this way ..
    Code:
    int[] ar1 = {1,2,3,4}
    int[] ar2 = {5,6,7,8}
    mine code :
    Code:
        public static void mergeArray(int[] ar1 , int[] ar2)    {
            int[] res = new int[ar1.length+ar2.length];
            int counter = 0;
            for(int a = 0; a<ar1.length; a++)
            {
                res[a] = ar1[a];
                counter++;
            }
            for(int b = 0; b<ar2.length; b++)
            {
                res[counter++] = ar2[b];
            }
            for(int temp = 0; temp<res.length;temp++)
            {
                System.out.print(res[temp]+" ");
            }
            
        }
    out put 12345678.

    all working .. then i was told that i can do it with 1 LOOP . curios if it is possible ?

    thanks for helping !

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    That looks like Java code. Do you have a question about C code?

  3. #3
    Registered User
    Join Date
    Jul 2018
    Posts
    10
    Oh sorry confused causethis forum in mine fast bookmarks.. Yea it is a Java

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    That's not a merge. You are just appending ar2 to ar1. Merging takes two sorted arrays and creates a third sorted array by comparing elements and putting them in order.

    I don't know Java, but going from your code maybe something like this (I'm assuming the arrays are dynamic and can be returned from a function):
    Code:
    public static void caller() {
        int[] ar1 = {1,3,5,7};
        int[] ar2 = {2,4,6};
        int[] res = mergeArray(ar1, ar2);
        for(int i = 0; i < res.length; i++)
            System.out.print(res[i] + " ");
    }
     
    public static int[] mergeArray(int[] ar1, int[] ar2) {
        int len = ar1.length + ar2.length, a = 0, b = 0;
        int[] res = new int[len];
        for (int i = 0; i < len; i++) {
            if (a < ar1.length && (b >= ar2.length || ar1[a] <= ar2[b]))
                res[i] = ar1[a++];
            else
                res[i] = ar2[b++];
        }
        return res;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loop merge optimizations
    By bot in forum C++ Programming
    Replies: 17
    Last Post: 12-03-2015, 03:03 PM
  2. to merge two arrays
    By Pulock2009 in forum C++ Programming
    Replies: 6
    Last Post: 02-15-2014, 09:24 AM
  3. Merge two arrays together
    By Fedryan in forum C Programming
    Replies: 2
    Last Post: 11-04-2013, 01:11 PM
  4. Can you merge arrays?
    By SeriousTyro in forum C Programming
    Replies: 14
    Last Post: 09-21-2009, 10:38 AM
  5. How to merge 2 arrays?
    By planet_abhi in forum C Programming
    Replies: 3
    Last Post: 02-16-2003, 12:23 AM

Tags for this Thread