Java Array Union union(int[] a, int[] b)

Here you can find the source of union(int[] a, int[] b)

Description

Returns a union of given sorted arrays.

License

Apache License

Declaration

public static int[] union(int[] a, int[] b) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Arrays;

public class Main {
    /** Union of given String arrays.
     */*from  w ww. j  a  v  a2 s . c o m*/
     * The method expects ordering of domains in given order (lexicographical, numeric)
     *
     * @param a first array
     * @param b second array
     * @param lexo - true if domains are sorted in lexicographical order or false for numeric domains
     * @return union of values in given arrays.
     *
     * precondition lexo ? a,b are lexicographically sorted : a,b are sorted numerically
     * precondition a!=null && b!=null
     */
    public static String[] union(String[] a, String[] b, boolean lexo) {
        assert a != null && b != null : "Union expect non-null input!";
        return union(a, b, 0, a.length, 0, b.length, lexo);
    }

    public static String[] union(String[] a, String[] b, int aoff,
            int alen, int boff, int blen, boolean lexo) {
        assert a != null && b != null : "Union expect non-null input!";
        String[] r = new String[alen + blen];
        int ia = aoff, ib = boff, i = 0;
        while (ia < aoff + alen && ib < boff + blen) {
            int c = lexo ? a[ia].compareTo(b[ib]) : Integer.valueOf(a[ia])
                    .compareTo(Integer.valueOf(b[ib]));
            if (c < 0)
                r[i++] = a[ia++];
            else if (c == 0) {
                r[i++] = a[ia++];
                ib++;
            } else
                r[i++] = b[ib++];
        }
        if (ia < aoff + alen)
            while (ia < aoff + alen)
                r[i++] = a[ia++];
        if (ib < boff + blen)
            while (ib < boff + blen)
                r[i++] = b[ib++];
        return Arrays.copyOf(r, i);
    }

    /** Returns a union of given sorted arrays. */
    public static int[] union(int[] a, int[] b) {
        assert a != null && b != null : "Union expect non-null input!";
        int[] r = new int[a.length + b.length];
        int ia = 0, ib = 0, i = 0;
        while (ia < a.length && ib < b.length) {
            int c = a[ia] - b[ib];
            if (c < 0)
                r[i++] = a[ia++];
            else if (c == 0) {
                r[i++] = a[ia++];
                ib++;
            } else
                r[i++] = b[ib++];
        }
        if (ia < a.length)
            while (ia < a.length)
                r[i++] = a[ia++];
        if (ib < b.length)
            while (ib < b.length)
                r[i++] = b[ib++];
        return Arrays.copyOf(r, i);
    }
}

Related

  1. domainUnion(String[] a, String[] b)
  2. domainUnion(String[] a, String[] b)
  3. getUnionStr(String[] strAry, String unionChar, String appendChar)
  4. union(int[]... arrays)
  5. unionOfStringArrays(String x[], String y[])