Java,找到两个数组的交集

我已经在这上面阅读了一些其他堆栈溢出线程:

在java中找到两个多重集的交集

如何将两个数组之间的交集作为新数组?

public static int[] intersection (int [] x, int numELementsInX, int [] y, int numElementsInY) { 

我试图检查两个数组以及它们的元素数(numElementsInX和numElementsInY),并返回一个新数组,其中包含数组x和y的公共值。 他们的交集。

 Example,if x is{1,3,5,7,9}and y is{9,3,9,4} then intersection(x, 5, y, 4} should return {3, 9} or {9, 3} 

我读过我需要使用LCS算法。 谁能给我一个如何做到这一点的例子? 数组中的数组和值都被初始化并在另一个方法中生成,然后传递到交集中。

任何帮助/澄清表示赞赏。

编辑代码

 for (int i=0; i<numElementsInX; i++){ for (int j=0; j<numElementsInY; j++){ if (x[j]==x[i]) { //how to push to new array?; } else{ } } } 

最简单的解决方案是使用集合,只要您不关心结果中的元素将具有不同的顺序,并且将删除重复项。 输入数组array1array2是给定int[]数组的Integer[]子数组,对应于您要处理的元素数:

 Set s1 = new HashSet(Arrays.asList(array1)); Set s2 = new HashSet(Arrays.asList(array2)); s1.retainAll(s2); Integer[] result = s1.toArray(new Integer[s1.size()]); 

上面将返回一个Integer[] ,如果需要,复制并将其内容转换为int[]

如果你对java-8很好,那么我能想到的最简单的解决方案是使用流和filter。 实施如下:

 public static int[] intersection(int[] a, int[] b) { return Arrays.stream(a) .distinct() .filter(x -> Arrays.stream(b).anyMatch(y -> y == x)) .toArray(); } 

如果您不想使用其他数据结构(如Set),那么基本思想是您希望迭代其中一个数组的元素,并为每个值查看它是否出现在另一个数组中。 你怎么看它是否出现在另一个arrays中? 遍历另一个数组中的元素,并为每个元素查看其值是否等于您要查找的值。 我怀疑你最好通过尝试自己解决这个问题,如果你上课的目的是学习编写Java,但是你遇到困难你可能会考虑用代码更新你的问题您已经写好了,这样您就可以获得更详细的反馈和指示。

尝试这个:

 public static void main(String[] args) { int[] arr1 = new int[]{1, 2, 3, 4, 5}; int[] arr2 = new int[]{3, 2, 5, 9, 11}; getIntersection(arr1, arr2); } public static Object[] getIntersection(int[] arr1, int[] arr2) { List list = new ArrayList(); for (int i = 0; i < arr1.length; i++) { for (int j = 0; j < arr2.length; j++) { if (arr1[i] == arr2[j]) { list.add(arr1[i]); } } } return list.toArray(); } 

在数组查找交集中使用重复元素。

  int [] arr1 = {1,2,2,2,2,2,2,3,6,6,6,6,6,6,}; int [] arr2 = {7,5,3,6,6,2,2,3,6,6,6,6,6,6,6,6,}; Arrays.sort(arr1); Arrays.sort(arr2); ArrayList result = new ArrayList<>(); int i =0 ; int j =0; while(i< arr1.length && jarr2[j]){ j++; }else if (arr1[i] 

使用哈希映射查找交集包括重复。

输出: 1 2 2 15 9 7 12

 public static void main(String[] args) { int[] arr1 = {1, 2, 2, 1, 5, 9, 15, 9, 7, 7, 12}; int[] arr2 = {1, 2, 2, 3, 4, 15, 9, 7, 12, 14}; printIntersect(arr1, arr2); } private static void printIntersect(int[] arr1, int[] arr2) { Map map = new HashMap<>(); //put first array to map for (int i = 0; i < arr1.length; i++) { if (!map.containsKey(arr1[i])) { map.put(arr1[i], 1); } else { map.put(arr1[i], map.get(arr1[i]) + 1); } } //check all value in array two for (int i = 0; i < arr2.length; i++) { //if exist and value>1 then decrement value //if value is 1 remove from map if (map.containsKey(arr2[i])) { System.out.print(arr2[i] + " "); if (map.get(arr2[i]) > 1) { map.put(arr2[i], map.get(arr2[i]) - 1); } else { map.remove(arr2[i]); } } } } 

如果数组已排序

  int a1[]=new int[] {1,2,3,5,7,8}; int a2[]=new int [] {1,5,6,7,8,9}; // get the length of both the array int n1=a1.length; int n2=a2.length; //create a new array to store the intersection int a3[]=new int[n1]; //run the loop and find the intersection int i=0,j=0,k=0; while(ia2[j]) { // a2 element at i are smaller than a2 element at j so increment j j++; }else { // intersection element store the value and increment i, j, k to find the next element a3[k]=a1[i]; i++; j++; k++; } } for(int l=0;l 
 if the arrays are not sorted int a1[]=new int[] {1,2,3,5,7,8}; int a2[]=new int [] {1,5,6,7,8,9}; // sort both the array Arrays.sort(a1); Arrays.sort(a2); // get the length of both the array int n1=a1.length; int n2=a2.length; //create a new array to store the intersection int a3[]=new int[n1]; //run the loop and find the intersection int i=0,j=0,k=0; while(ia2[j]) { // a2 element at i are smaller than a2 element at j so increment j j++; }else { // intersection element store the value and increment i, j, k to find the next element a3[k]=a1[i]; i++; j++; k++; } } for(int l=0;l