@Test public void test() { Random r = new Random(); for (int k = 0; k < 10; k++) { Set<Integer> s1 = new HashSet<>(), s2 = new HashSet<>(); int N = 100000; for (int i = 0; i < N; i++) s1.add(r.nextInt(N * 2)); for (int i = 0; i < N; i++) s2.add(r.nextInt(N * 2)); int cnt = 0; int[] arr1 = new int[s1.size()]; int[] arr2 = new int[s2.size()]; int idx = 0; for (int i : s1) { arr1[idx++] = i; if (s2.contains(i)) cnt++; } idx = 0; for (int i : s2) { arr2[idx++] = i; } Assert.assertEquals(cnt, IntersectionOfTwoSet.intersectionCount(arr1, arr2)); } }
public static int intersectionCount(int[] a, int[] b) { ISorter shellSorter = new ShellSorter(); shellSorter.sort(a); shellSorter.sort(b); int cnt = 0; int i = 0, j = 0; for (; i < a.length && j < b.length; ) { if (a[i] < b[j]) i++; else if (a[i] > b[j]) j++; else { i++; j++; cnt++; } } return cnt; }
IntersectionOfTwoSet { public static int intersectionCount(int[] a, int[] b) { ISorter shellSorter = new ShellSorter(); shellSorter.sort(a); shellSorter.sort(b); int cnt = 0; int i = 0, j = 0; for (; i < a.length && j < b.length; ) { if (a[i] < b[j]) i++; else if (a[i] > b[j]) j++; else { i++; j++; cnt++; } } return cnt; } }
IntersectionOfTwoSet { public static int intersectionCount(int[] a, int[] b) { ISorter shellSorter = new ShellSorter(); shellSorter.sort(a); shellSorter.sort(b); int cnt = 0; int i = 0, j = 0; for (; i < a.length && j < b.length; ) { if (a[i] < b[j]) i++; else if (a[i] > b[j]) j++; else { i++; j++; cnt++; } } return cnt; }  }
IntersectionOfTwoSet { public static int intersectionCount(int[] a, int[] b) { ISorter shellSorter = new ShellSorter(); shellSorter.sort(a); shellSorter.sort(b); int cnt = 0; int i = 0, j = 0; for (; i < a.length && j < b.length; ) { if (a[i] < b[j]) i++; else if (a[i] > b[j]) j++; else { i++; j++; cnt++; } } return cnt; }  static int intersectionCount(int[] a, int[] b); }
IntersectionOfTwoSet { public static int intersectionCount(int[] a, int[] b) { ISorter shellSorter = new ShellSorter(); shellSorter.sort(a); shellSorter.sort(b); int cnt = 0; int i = 0, j = 0; for (; i < a.length && j < b.length; ) { if (a[i] < b[j]) i++; else if (a[i] > b[j]) j++; else { i++; j++; cnt++; } } return cnt; }  static int intersectionCount(int[] a, int[] b);  }