Finding common elements in two arrays of different size

后端 未结 10 845
隐瞒了意图╮
隐瞒了意图╮ 2020-12-14 18:08

I have a problem to find common elements in two arrays and that\'s of different size.

Take , Array A1 of size n and Array A2 o

10条回答
  •  攒了一身酷
    2020-12-14 18:30

    I solve the problem by using Set intersection. It is very elegant. Even though I did not analyze the time complexity, it is probably in reasonable range.

    public Set FindCommonElements(Integer[] first, Integer[] second)
    {
    
        Set set1=new HashSet(Arrays.asList(first));
        Set set2=new HashSet(Arrays.asList(second));
    
        // finds intersecting elements in two sets
        set1.retainAll(set2);
    
        return set1;
    
    }
    

提交回复
热议问题