Here you can find the source of unique(int[] a, int aLen, int[] b, int bLen)
Parameter | Description |
---|---|
a | First array. |
aLen | Length of prefix a . |
b | Second array. |
bLen | Length of prefix b . |
@SuppressWarnings("IfMayBeConditional") public static int[] unique(int[] a, int aLen, int[] b, int bLen)
//package com.java2s; /* //from w w w . j a va2 s. com Copyright (C) GridGain Systems. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import java.util.*; public class Main { /** * Returns array which is the union of two arrays * (array of elements contained in any of provided arrays). * <p/> * Note: arrays must be increasing. * * @param a First array. * @param aLen Length of prefix {@code a}. * @param b Second array. * @param bLen Length of prefix {@code b}. * @return Increasing array which is union of {@code a} and {@code b}. */ @SuppressWarnings("IfMayBeConditional") public static int[] unique(int[] a, int aLen, int[] b, int bLen) { assert a != null; assert b != null; assert isIncreasingArray(a, aLen); assert isIncreasingArray(b, bLen); int[] res = new int[aLen + bLen]; int resLen = 0; int i = 0; int j = 0; while (i < aLen && j < bLen) { if (a[i] == b[j]) i++; else if (a[i] < b[j]) res[resLen++] = a[i++]; else res[resLen++] = b[j++]; } while (i < aLen) res[resLen++] = a[i++]; while (j < bLen) res[resLen++] = b[j++]; return copyIfExceeded(res, resLen); } /** * Checks if array prefix increases. * * @param arr Array. * @param len Prefix length. * @return {@code True} if {@code arr} from 0 to ({@code len} - 1) increases. */ public static boolean isIncreasingArray(int[] arr, int len) { assert arr != null; assert 0 <= len && len <= arr.length; if (arr.length == 0) return true; for (int i = 1; i < len; i++) { if (arr[i - 1] >= arr[i]) return false; } return true; } /** * Copies array only if array length greater than needed length. * * @param arr Array. * @param len Prefix length. * @return Old array if length of {@code arr} is equals to {@code len}, * otherwise copy of array. */ public static int[] copyIfExceeded(int[] arr, int len) { assert arr != null; assert 0 <= len && len <= arr.length; return len == arr.length ? arr : Arrays.copyOf(arr, len); } }