Java Array Intersect intersect(int[] sorted1, int[] sorted2)

Here you can find the source of intersect(int[] sorted1, int[] sorted2)

Description

intersect

License

Open Source License

Declaration

public static int[] intersect(int[] sorted1, int[] sorted2) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.Arrays;

public class Main {
    public static int[] intersect(int[] sorted1, int[] sorted2) {
        int[] result = new int[Math.min(sorted1.length, sorted2.length)];
        int i = 0, j = 0, k = 0;

        while (i < sorted1.length && j < sorted2.length) {
            if (sorted1[i] < sorted2[j])
                i++;//from  ww w  . j  a va2  s.c  om
            else if (sorted1[i] > sorted2[j])
                j++;
            else {
                result[k++] = sorted1[i];
                i++;
                j++;
            }
        }

        return Arrays.copyOf(result, k);
    }
}

Related

  1. byteIntersection(byte[] a, byte[] b)
  2. getArrayIntersection(int a[], int b[])
  3. getNonIntersection(int[] interval, int[] intervalToRemove)
  4. hasIntersection(String a1[], String a2[], int mode)
  5. intersect(boolean[] mask, int[] examples, boolean[] intersection)
  6. intersect(int[]... arrays)
  7. intersect(String[] arr1, String[] arr2)
  8. intersect(String[] list1, String[] list2)
  9. intersect(T[] a, T[] b)