Shaibayan Chakrabarti
Shaibayan Chakrabarti

Reputation: 55

Merge two arrays in Java to maintain the order and with the unique blank values of first array

I have two arrays. I need the blanks for later code.

Both the arrays will have common values and the common values are expected to be in order.

Here we have Alpha, Beta, Phi, and Gamma. There are blanks in between in both arrays.

String[] a = { " ", "Alpha", "Beta", "Phi", " ", "Gamma" };
String[] b = { "Alpha", " ", "Beta", "Phi", "Gamma", " " };

The output should consider the first array i.e. since the first element of the array, a is " " so the output starts with Blank. The Blank between Alpha and Beta of the second array and the one after Gamma should remain unaltered as that is part of the second array. Additionally since in the first array, a there is another " " between Phi and Gamma which does not occur in the second array, b hence that is also added in the output. The order of both arrays must remain the same but the positions will change due to the insertion of " ".

Expected output:

[ " ", "Alpha", " ", "Beta", "Phi", " ", "Gamma", " " ]

Normal merging is simple and a lot of examples are out there. However, this is something that I am unable to figure out yet.

Any hints or examples will be helpful.

Apologies this somehow turns out to be a duplicate post.

Upvotes: 0

Views: 624

Answers (1)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79540

You can do it as follows:

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int i, j, k;
        String[] a = { " ", "Alpha", "Beta", "Phi", " ", "Gamma" };
        String[] b = { "Alpha", " ", "Beta", "Phi", "Gamma", " " };

        // Target array
        String[] c = new String[a.length + b.length];

        // Copy the elements from both the source arrays to the target array
        for (i = 0; i < a.length && i < b.length; i++) {
            add(c, a[i]);
            add(c, b[i]);
        }

        // Copy the remaining elements from the first source array
        for (k = i; k < a.length; k++) {
            add(c, a[k]);
        }

        // Copy the remaining elements from the second source array
        for (k = i; k < b.length; k++) {
            add(c, b[k]);
        }

        // Discard null elements
        c = trim(c);

        // Display the target array
        System.out.println(Arrays.toString(c));
    }

    static void add(String[] arr, String s) {
        int i;
        if (" ".equals(s)) {
            // Find the position of first null
            for (i = 0; arr[i] != null && i < arr.length; i++) {
                // Do nothing
            }

            // Replace null with " "
            arr[i] = s;
            return;
        }

        // Return if the string already exists
        for (i = 0; arr[i] != null && i < arr.length; i++) {
            if (arr[i].equals(s)) {
                return;
            }
        }

        // Replace null with the string
        arr[i] = s;
    }

    static String[] trim(String[] arr) {
        int i;

        // Find the position of first null
        for (i = 0; arr[i] != null && i < arr.length; i++) {
            // Do nothing
        }

        return Arrays.copyOf(arr, i);
    }
}

Output:

[ , Alpha,  , Beta, Phi,  , Gamma,  ]

Upvotes: 0

Related Questions