Finding duplicate values between two arrays

前端 未结 6 1039
死守一世寂寞
死守一世寂寞 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:16

    You just need two nested for loops

    for(int i = 0; i < a.length; i++)
    {
        for(int j = 0; j < b.length; j++)
        {
            if(a[i] == b[j])
            {
                //value is in both arrays
            }
        }
    }
    

    What this does is go to the first value of a and compare to each value in b, then go to the next value of a and repeat.

提交回复
热议问题