Finding duplicate values between two arrays

前端 未结 6 1195
死守一世寂寞
死守一世寂寞 2020-12-09 22:56

Let\'s say I have the following two arrays:

int[] a = [1,2,3,4,5];
int[] b = [8,1,3,9,4];

I would like to take the first value of array

6条回答
  •  盖世英雄少女心
    2020-12-09 23:18

    //O(n log(n)), Linear Space Complexity  
    
    void findDuplicates(int[] x, int[] y){
        Arrays.sort(x);
        Arrays.sort(y);
        int i = 0,j = 0;
        while (i < x.length && j < y.length) {
            if(x[i] == y[j]){
                System.out.println(x[i]);
                i++;
                j++;
            }else if (x[i] < y[j])
                i++;
            else
                j++;
        }
    }
    

提交回复
热议问题