Intersection and union of ArrayLists in Java

后端 未结 24 2170
离开以前
离开以前 2020-11-22 06:05

Are there any methods to do so? I was looking but couldn\'t find any.

Another question: I need these methods so I can filter files. Some are AND filter

24条回答
  •  有刺的猬
    2020-11-22 06:45

    First, I am copying all values of arrays into a single array then I am removing duplicates values into the array. Line 12, explaining if same number occur more than time then put some extra garbage value into "j" position. At the end, traverse from start-end and check if same garbage value occur then discard.

    public class Union {
    public static void main(String[] args){
    
        int arr1[]={1,3,3,2,4,2,3,3,5,2,1,99};
        int arr2[]={1,3,2,1,3,2,4,6,3,4};
        int arr3[]=new int[arr1.length+arr2.length];
    
        for(int i=0;i

提交回复
热议问题